home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / stk-3.002 / stk-3 / STk-3.1 / Tk / win / tkWinWm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-26  |  97.1 KB  |  3,436 lines

  1. /* 
  2.  * tkWinWm.c --
  3.  *
  4.  *    This module takes care of the interactions between a Tk-based
  5.  *    application and the window manager.  Among other things, it
  6.  *    implements the "wm" command and passes geometry information
  7.  *    to the window manager.
  8.  *
  9.  * Copyright (c) 1995 Sun Microsystems, Inc.
  10.  *
  11.  * See the file "license.terms" for information on usage and redistribution
  12.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13.  *
  14.  * SCCS: @(#) tkWinWm.c 1.33 96/04/16 15:08:49
  15.  */
  16.  
  17. #include "tkWinInt.h"
  18.  
  19. /*
  20.  * A data structure of the following type holds information for
  21.  * each window manager protocol (such as WM_DELETE_WINDOW) for
  22.  * which a handler (i.e. a Tcl command) has been defined for a
  23.  * particular top-level window.
  24.  */
  25.  
  26. typedef struct ProtocolHandler {
  27.     Atom protocol;        /* Identifies the protocol. */
  28.     struct ProtocolHandler *nextPtr;
  29.                 /* Next in list of protocol handlers for
  30.                  * the same top-level window, or NULL for
  31.                  * end of list. */
  32.     Tcl_Interp *interp;        /* Interpreter in which to invoke command. */
  33.     char command[4];        /* Tcl command to invoke when a client
  34.                  * message for this protocol arrives. 
  35.                  * The actual size of the structure varies
  36.                  * to accommodate the needs of the actual
  37.                  * command. THIS MUST BE THE LAST FIELD OF
  38.                  * THE STRUCTURE. */
  39. } ProtocolHandler;
  40.  
  41. #define HANDLER_SIZE(cmdLength) \
  42.     ((unsigned) (sizeof(ProtocolHandler) - 3 + cmdLength))
  43.  
  44. /*
  45.  * A data structure of the following type holds window-manager-related
  46.  * information for each top-level window in an application.
  47.  */
  48.  
  49. typedef struct TkWmInfo {
  50.     TkWindow *winPtr;        /* Pointer to main Tk information for
  51.                  * this window. */
  52.     Window reparent;        /* This is the decorative frame window
  53.                  * created by the window manager to wrap
  54.                  * a toplevel window.  This window is
  55.                  * a direct child of the root window. */
  56.     Tk_Uid titleUid;        /* Title to display in window caption.  If
  57.                  * NULL, use name of widget. */
  58.     Tk_Uid iconName;        /* Name to display in icon. */
  59.     Window master;        /* Master window for TRANSIENT_FOR property,
  60.                  * or None. */
  61.     XWMHints hints;        /* Various pieces of information for
  62.                  * window manager. */
  63.     Tk_Uid leaderName;        /* Path name of leader of window group
  64.                  * (corresponds to hints.window_group).
  65.                  * Note:  this field doesn't get updated
  66.                  * if leader is destroyed. */
  67.     Tk_Uid masterWindowName;    /* Path name of window specified as master
  68.                  * in "wm transient" command, or NULL.
  69.                  * Note:  this field doesn't get updated if
  70.                  * masterWindowName is destroyed. */
  71.     Tk_Window icon;        /* Window to use as icon for this window,
  72.                  * or NULL. */
  73.     Tk_Window iconFor;        /* Window for which this window is icon, or
  74.                  * NULL if this isn't an icon for anyone. */
  75.     int withdrawn;        /* Non-zero means window has been withdrawn. */
  76.  
  77.     /*
  78.      * Information used to construct an XSizeHints structure for
  79.      * the window manager:
  80.      */
  81.  
  82.     int defMinWidth, defMinHeight, defMaxWidth, defMaxHeight;
  83.                 /* Default resize limits given by system. */
  84.     int sizeHintsFlags;        /* Flags word for XSizeHints structure.
  85.                  * If the PBaseSize flag is set then the
  86.                  * window is gridded;  otherwise it isn't
  87.                  * gridded. */
  88.     int minWidth, minHeight;    /* Minimum dimensions of window, in
  89.                  * grid units, not pixels. */
  90.     int maxWidth, maxHeight;    /* Maximum dimensions of window, in
  91.                  * grid units, not pixels, or 0 to default. */
  92.     Tk_Window gridWin;        /* Identifies the window that controls
  93.                  * gridding for this top-level, or NULL if
  94.                  * the top-level isn't currently gridded. */
  95.     int widthInc, heightInc;    /* Increments for size changes (# pixels
  96.                  * per step). */
  97.     struct {
  98.     int x;    /* numerator */
  99.     int y;  /* denominator */
  100.     } minAspect, maxAspect;    /* Min/max aspect ratios for window. */
  101.     int reqGridWidth, reqGridHeight;
  102.                 /* The dimensions of the window (in
  103.                  * grid units) requested through
  104.                  * the geometry manager. */
  105.     int gravity;        /* Desired window gravity. */
  106.  
  107.     /*
  108.      * Information used to manage the size and location of a window.
  109.      */
  110.  
  111.     int width, height;        /* Desired dimensions of window, specified
  112.                  * in grid units.  These values are
  113.                  * set by the "wm geometry" command and by
  114.                  * ConfigureNotify events (for when wm
  115.                  * resizes window).  -1 means user hasn't
  116.                  * requested dimensions. */
  117.     int x, y;            /* Desired X and Y coordinates for window.
  118.                  * These values are set by "wm geometry",
  119.                  * plus by ConfigureNotify events (when wm
  120.                  * moves window).  These numbers are
  121.                  * different than the numbers stored in
  122.                  * winPtr->changes because (a) they could be
  123.                  * measured from the right or bottom edge
  124.                  * of the screen (see WM_NEGATIVE_X and
  125.                  * WM_NEGATIVE_Y flags) and (b) if the window
  126.                  * has been reparented then they refer to the
  127.                  * parent rather than the window itself. */
  128.     int xInParent, yInParent;    /* Offset of window within reparent,  measured
  129.                  * from upper-left outer corner of parent's
  130.                  * border to upper-left outer corner of child's
  131.                  * border.  If not reparented then these are
  132.                  * zero. */
  133.     int borderWidth, borderHeight;
  134.                 /* Width and height of window dressing, in
  135.                  * pixels for the current style/exStyle.  This
  136.                  * includes the border on both sides of the
  137.                  * window. */
  138.     int configWidth, configHeight;
  139.                 /* Dimensions passed to last request that we
  140.                  * issued to change geometry of window.  Used
  141.                  * to eliminate redundant resize operations. */
  142.     long style;            /* Window style of reparent. */
  143.     long exStyle;        /* Window exStyle of reparent. */
  144.  
  145.     /*
  146.      * List of children of the toplevel which have private colormaps.
  147.      */
  148.  
  149.     TkWindow **cmapList;    /* Array of window with private colormaps. */
  150.     int cmapCount;        /* Number of windows in array. */
  151.  
  152.     /*
  153.      * Miscellaneous information.
  154.      */
  155.  
  156.     ProtocolHandler *protPtr;    /* First in list of protocol handlers for
  157.                  * this window (NULL means none). */
  158.     int cmdArgc;        /* Number of elements in cmdArgv below. */
  159.     char **cmdArgv;        /* Array of strings to store in the
  160.                  * WM_COMMAND property.  NULL means nothing
  161.                  * available. */
  162.     char *clientMachine;    /* String to store in WM_CLIENT_MACHINE
  163.                  * property, or NULL. */
  164.     int flags;            /* Miscellaneous flags, defined below. */
  165.     struct TkWmInfo *nextPtr;    /* Next in list of all top-level windows. */
  166. } WmInfo;
  167.  
  168. /*
  169.  * Flag values for WmInfo structures:
  170.  *
  171.  * WM_NEVER_MAPPED -        non-zero means window has never been
  172.  *                mapped;  need to update all info when
  173.  *                window is first mapped.
  174.  * WM_UPDATE_PENDING -        non-zero means a call to UpdateGeometryInfo
  175.  *                has already been scheduled for this
  176.  *                window;  no need to schedule another one.
  177.  * WM_NEGATIVE_X -        non-zero means x-coordinate is measured in
  178.  *                pixels from right edge of screen, rather
  179.  *                than from left edge.
  180.  * WM_NEGATIVE_Y -        non-zero means y-coordinate is measured in
  181.  *                pixels up from bottom of screen, rather than
  182.  *                down from top.
  183.  * WM_UPDATE_SIZE_HINTS -    non-zero means that new size hints need to be
  184.  *                propagated to window manager.
  185.  * WM_SYNC_PENDING -        set to non-zero while waiting for the window
  186.  *                manager to respond to some state change.
  187.  * WM_MOVE_PENDING -        non-zero means the application has requested
  188.  *                a new position for the window, but it hasn't
  189.  *                been reflected through the window manager
  190.  *                yet.
  191.  * WM_COLORAMPS_EXPLICIT -    non-zero means the colormap windows were
  192.  *                set explicitly via "wm colormapwindows".
  193.  * WM_ADDED_TOPLEVEL_COLORMAP - non-zero means that when "wm colormapwindows"
  194.  *                was called the top-level itself wasn't
  195.  *                specified, so we added it implicitly at
  196.  *                the end of the list.
  197.  */
  198.  
  199. #define WM_NEVER_MAPPED            (1<<0)
  200. #define WM_UPDATE_PENDING        (1<<1)
  201. #define WM_NEGATIVE_X            (1<<2)
  202. #define WM_NEGATIVE_Y            (1<<3)
  203. #define WM_UPDATE_SIZE_HINTS        (1<<4)
  204. #define WM_SYNC_PENDING            (1<<5)
  205. #define WM_CREATE_PENDING        (1<<6)
  206. #define WM_MOVE_PENDING            (1<<7)
  207. #define WM_COLORMAPS_EXPLICIT        (1<<8)
  208. #define WM_ADDED_TOPLEVEL_COLORMAP    (1<<9)
  209. #define WM_WIDTH_NOT_RESIZABLE        (1<<10)
  210. #define WM_HEIGHT_NOT_RESIZABLE        (1<<11)
  211.  
  212. /*
  213.  * Window styles for various types of toplevel windows.
  214.  */
  215.  
  216. #define WM_TOPLEVEL_STYLE (WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|CS_DBLCLKS)
  217. #define WM_OVERRIDE_STYLE (WS_POPUP|WS_CLIPCHILDREN|CS_DBLCLKS)
  218. #define WM_TRANSIENT_STYLE \
  219.         (WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_CLIPSIBLINGS|CS_DBLCLKS)
  220.  
  221. #define EX_TOPLEVEL_STYLE 0;
  222. #define EX_OVERRIDE_STYLE (WS_EX_TOOLWINDOW);
  223. #define EX_TRANSIENT_STYLE (WS_EX_TOOLWINDOW | WS_EX_DLGMODALFRAME);
  224.  
  225. /*
  226.  * This module keeps a list of all top-level windows.
  227.  */
  228.  
  229. static WmInfo *firstWmPtr = NULL;    /* Points to first top-level window. */
  230. static WmInfo *foregroundWmPtr = NULL; /* Points to the foreground window. */
  231.  
  232. /*
  233.  * The variable below is used to enable or disable tracing in this
  234.  * module.  If tracing is enabled, then information is printed on
  235.  * standard output about interesting interactions with the window
  236.  * manager.
  237.  */
  238.  
  239. static int wmTracing = 0;
  240.  
  241. /*
  242.  * The following structure is the official type record for geometry
  243.  * management of top-level windows.
  244.  */
  245.  
  246. static void        TopLevelReqProc _ANSI_ARGS_((ClientData dummy,
  247.                 Tk_Window tkwin));
  248.  
  249. static Tk_GeomMgr wmMgrType = {
  250.     "wm",                /* name */
  251.     TopLevelReqProc,            /* requestProc */
  252.     (Tk_GeomLostSlaveProc *) NULL,    /* lostSlaveProc */
  253. };
  254.  
  255. /*
  256.  * Global system palette.  This value always refers to the currently
  257.  * installed foreground logical palette.
  258.  */
  259.  
  260. static HPALETTE systemPalette = NULL;
  261.  
  262. /*
  263.  * Window that is being constructed.  This value is set immediately
  264.  * before a call to CreateWindowEx, and is used by TkWinWmSetLimits.
  265.  * This is a gross hack needed to work around Windows brain damage
  266.  * where it sends the WM_GETMINMAXINFO message before the WM_CREATE
  267.  * window.
  268.  */
  269.  
  270. static TkWindow *createWindow = NULL;
  271.  
  272. /*
  273.  * Forward declarations for procedures defined in this file:
  274.  */
  275.  
  276. static void        ConfigureEvent _ANSI_ARGS_((TkWindow *winPtr,
  277.                 XConfigureEvent *eventPtr));
  278. static void        DeiconifyWindow _ANSI_ARGS_((TkWindow *winPtr));
  279. static void        GetMaxSize _ANSI_ARGS_((WmInfo *wmPtr,
  280.                 int *maxWidthPtr, int *maxHeightPtr));
  281. static void        GetMinSize _ANSI_ARGS_((WmInfo *wmPtr,
  282.                 int *minWidthPtr, int *minHeightPtr));
  283. static void        IconifyWindow _ANSI_ARGS_((TkWindow *winPtr));
  284. static void        InvalidateSubTree _ANSI_ARGS_((TkWindow *winPtr,
  285.                     Colormap colormap));
  286. static int        ParseGeometry _ANSI_ARGS_((Tcl_Interp *interp,
  287.                 char *string, TkWindow *winPtr));
  288. static void        RefreshColormap _ANSI_ARGS_((Colormap colormap));
  289. static void        TopLevelEventProc _ANSI_ARGS_((ClientData clientData,
  290.                 XEvent *eventPtr));
  291. static void        TopLevelReqProc _ANSI_ARGS_((ClientData dummy,
  292.                 Tk_Window tkwin));
  293. static void        UpdateGeometryInfo _ANSI_ARGS_((
  294.                 ClientData clientData));
  295.  
  296. /*
  297.  *----------------------------------------------------------------------
  298.  *
  299.  * TkWinWmSetLimits --
  300.  *
  301.  *    Updates the minimum and maximum window size constraints.
  302.  *
  303.  * Results:
  304.  *    None.
  305.  *
  306.  * Side effects:
  307.  *    Changes the values of the info pointer to reflect the current
  308.  *    minimum and maximum size values.
  309.  *
  310.  *----------------------------------------------------------------------
  311.  */
  312.  
  313. void
  314. TkWinWmSetLimits(hwnd, info)
  315.     HWND hwnd;
  316.     MINMAXINFO *info;
  317. {
  318.     TkWinDrawable *twdPtr;
  319.     TkWindow *winPtr;
  320.     register WmInfo *wmPtr;
  321.     int maxWidth, maxHeight;
  322.     int minWidth, minHeight;
  323.     int base;
  324.  
  325.     /*
  326.      * Get the window from the user data slot or from the createWindow.
  327.      */
  328.  
  329.     twdPtr = (TkWinDrawable *) GetWindowLong(hwnd, GWL_USERDATA);
  330.     if (twdPtr != NULL ) {
  331.     winPtr = TkWinGetWinPtr(twdPtr);
  332.     } else {
  333.     winPtr = createWindow;
  334.     }    
  335.  
  336.     if (winPtr == NULL) {
  337.     return;
  338.     }
  339.  
  340.     wmPtr = winPtr->wmInfoPtr;
  341.     
  342.     /*
  343.      * Copy latest constraint info.
  344.      */
  345.  
  346.     wmPtr->defMinWidth = info->ptMinTrackSize.x;
  347.     wmPtr->defMinHeight = info->ptMinTrackSize.y;
  348.     wmPtr->defMaxWidth = info->ptMaxTrackSize.x;
  349.     wmPtr->defMaxHeight = info->ptMaxTrackSize.y;
  350.     
  351.     GetMaxSize(wmPtr, &maxWidth, &maxHeight);
  352.     GetMinSize(wmPtr, &minWidth, &minHeight);
  353.  
  354.     if (wmPtr->gridWin != NULL) {
  355.     base = winPtr->reqWidth - (wmPtr->reqGridWidth * wmPtr->widthInc);
  356.     if (base < 0) {
  357.         base = 0;
  358.     }
  359.     base += wmPtr->borderWidth;
  360.     info->ptMinTrackSize.x = base + (minWidth * wmPtr->widthInc);
  361.     info->ptMaxTrackSize.x = base + (maxWidth * wmPtr->widthInc);
  362.  
  363.     base = winPtr->reqHeight - (wmPtr->reqGridHeight * wmPtr->heightInc);
  364.     if (base < 0) {
  365.         base = 0;
  366.     }
  367.     base += wmPtr->borderHeight;
  368.     info->ptMinTrackSize.y = base + (minHeight * wmPtr->heightInc);
  369.     info->ptMaxTrackSize.y = base + (maxHeight * wmPtr->heightInc);
  370.     } else {
  371.     info->ptMaxTrackSize.x = maxWidth + wmPtr->borderWidth;
  372.     info->ptMaxTrackSize.y = maxHeight + wmPtr->borderHeight;
  373.     info->ptMinTrackSize.x = minWidth + wmPtr->borderWidth;
  374.     info->ptMinTrackSize.y = minHeight + wmPtr->borderHeight;
  375.     }
  376.  
  377.     /*
  378.      * If the window isn't supposed to be resizable, then set the
  379.      * minimum and maximum dimensions to be the same as the current size.
  380.      */
  381.  
  382.     if (wmPtr->flags & WM_WIDTH_NOT_RESIZABLE) {
  383.     info->ptMinTrackSize.x = winPtr->changes.width + wmPtr->borderWidth;
  384.     info->ptMaxTrackSize.x = info->ptMinTrackSize.x;
  385.     }
  386.     if (wmPtr->flags & WM_HEIGHT_NOT_RESIZABLE) {
  387.     info->ptMinTrackSize.y = winPtr->changes.height + wmPtr->borderHeight;
  388.     info->ptMaxTrackSize.y = info->ptMinTrackSize.y;
  389.     }
  390. }
  391.  
  392. /*
  393.  *--------------------------------------------------------------
  394.  *
  395.  * TkWmNewWindow --
  396.  *
  397.  *    This procedure is invoked whenever a new top-level
  398.  *    window is created.  Its job is to initialize the WmInfo
  399.  *    structure for the window.
  400.  *
  401.  * Results:
  402.  *    None.
  403.  *
  404.  * Side effects:
  405.  *    A WmInfo structure gets allocated and initialized.
  406.  *
  407.  *--------------------------------------------------------------
  408.  */
  409.  
  410. void
  411. TkWmNewWindow(winPtr)
  412.     TkWindow *winPtr;        /* Newly-created top-level window. */
  413. {
  414.     register WmInfo *wmPtr;
  415.  
  416.     wmPtr = (WmInfo *) ckalloc(sizeof(WmInfo));
  417.     winPtr->wmInfoPtr = wmPtr;
  418.     wmPtr->winPtr = winPtr;
  419.     wmPtr->reparent = None;
  420.     wmPtr->titleUid = NULL;
  421.     wmPtr->iconName = NULL;
  422.     wmPtr->master = None;
  423.     wmPtr->hints.flags = InputHint | StateHint;
  424.     wmPtr->hints.input = True;
  425.     wmPtr->hints.initial_state = NormalState;
  426.     wmPtr->hints.icon_pixmap = None;
  427.     wmPtr->hints.icon_window = None;
  428.     wmPtr->hints.icon_x = wmPtr->hints.icon_y = 0;
  429.     wmPtr->hints.icon_mask = None;
  430.     wmPtr->hints.window_group = None;
  431.     wmPtr->leaderName = NULL;
  432.     wmPtr->masterWindowName = NULL;
  433.     wmPtr->icon = NULL;
  434.     wmPtr->iconFor = NULL;
  435.     wmPtr->withdrawn = 0;
  436.     wmPtr->sizeHintsFlags = 0;
  437.  
  438.     /*
  439.      * Default the maximum dimensions to the size of the display.
  440.      */
  441.  
  442.     wmPtr->defMinWidth = wmPtr->defMinHeight = 0;
  443.     wmPtr->defMaxWidth = DisplayWidth(winPtr->display,
  444.         winPtr->screenNum);
  445.     wmPtr->defMaxHeight = DisplayHeight(winPtr->display,
  446.         winPtr->screenNum);
  447.     wmPtr->minWidth = wmPtr->minHeight = 1;
  448.     wmPtr->maxWidth = wmPtr->maxHeight = 0;
  449.     wmPtr->gridWin = NULL;
  450.     wmPtr->widthInc = wmPtr->heightInc = 1;
  451.     wmPtr->minAspect.x = wmPtr->minAspect.y = 1;
  452.     wmPtr->maxAspect.x = wmPtr->maxAspect.y = 1;
  453.     wmPtr->reqGridWidth = wmPtr->reqGridHeight = -1;
  454.     wmPtr->gravity = NorthWestGravity;
  455.     wmPtr->width = -1;
  456.     wmPtr->height = -1;
  457.     wmPtr->style = WM_TOPLEVEL_STYLE;
  458.     wmPtr->exStyle = EX_TOPLEVEL_STYLE;
  459.     wmPtr->x = winPtr->changes.x;
  460.     wmPtr->y = winPtr->changes.y;
  461.     wmPtr->borderWidth = 0;
  462.     wmPtr->borderHeight = 0;
  463.     wmPtr->xInParent = 0;
  464.     wmPtr->yInParent = 0;
  465.     
  466.     wmPtr->cmapList = NULL;
  467.     wmPtr->cmapCount = 0;
  468.  
  469.     wmPtr->configWidth = -1;
  470.     wmPtr->configHeight = -1;
  471.     wmPtr->protPtr = NULL;
  472.     wmPtr->cmdArgv = NULL;
  473.     wmPtr->clientMachine = NULL;
  474.     wmPtr->flags = WM_NEVER_MAPPED;
  475.     wmPtr->nextPtr = firstWmPtr;
  476.     firstWmPtr = wmPtr;
  477.  
  478.     /*
  479.      * Tk must monitor structure events for top-level windows, in order
  480.      * to detect size and position changes caused by window managers.
  481.      */
  482.  
  483.     Tk_CreateEventHandler((Tk_Window) winPtr, StructureNotifyMask,
  484.         TopLevelEventProc, (ClientData) winPtr);
  485.  
  486.     /*
  487.      * Arrange for geometry requests to be reflected from the window
  488.      * to the window manager.
  489.      */
  490.  
  491.     Tk_ManageGeometry((Tk_Window) winPtr, &wmMgrType, (ClientData) 0);
  492. }
  493.  
  494. /*
  495.  *--------------------------------------------------------------
  496.  *
  497.  * TkWmMapWindow --
  498.  *
  499.  *    This procedure is invoked to map a top-level window.  This
  500.  *    module gets a chance to update all window-manager-related
  501.  *    information in properties before the window manager sees
  502.  *    the map event and checks the properties.  It also gets to
  503.  *    decide whether or not to even map the window after all.
  504.  *
  505.  * Results:
  506.  *    None.
  507.  *
  508.  * Side effects:
  509.  *    Properties of winPtr may get updated to provide up-to-date
  510.  *    information to the window manager.  The window may also get
  511.  *    mapped, but it may not be if this procedure decides that
  512.  *    isn't appropriate (e.g. because the window is withdrawn).
  513.  *
  514.  *--------------------------------------------------------------
  515.  */
  516.  
  517. void
  518. TkWmMapWindow(winPtr)
  519.     TkWindow *winPtr;        /* Top-level window that's about to
  520.                  * be mapped. */
  521. {
  522.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  523.     XEvent event;
  524.     HWND child = TkWinGetHWND(winPtr->window);
  525.  
  526.     if (wmPtr->flags & WM_NEVER_MAPPED) {
  527.     int x, y, width, height;
  528.     TkWinDrawable *parentPtr;
  529.     RECT rect;
  530.     HWND window, parentHWND = NULL;
  531.  
  532.     wmPtr->flags &= ~WM_NEVER_MAPPED;
  533.  
  534.     /*
  535.      * This is the first time this window has ever been mapped.
  536.      * Store all the window-manager-related information for the
  537.      * window.
  538.      */
  539.  
  540.     if (wmPtr->titleUid == NULL) {
  541.         wmPtr->titleUid = winPtr->nameUid;
  542.     }
  543.  
  544.     /*
  545.      * Pick the decorative frame style.  Override redirect windows get
  546.      * created as undecorated popups.  Transient windows get a modal dialog
  547.      * frame.  Neither override, nor transient windows appear in the Win95
  548.      * taskbar.
  549.      */
  550.     
  551.     if (winPtr->atts.override_redirect) {
  552.         wmPtr->style = WM_OVERRIDE_STYLE;
  553.         wmPtr->exStyle = EX_OVERRIDE_STYLE;
  554.     } else if (wmPtr->master) {
  555.         wmPtr->style = WM_TRANSIENT_STYLE;
  556.         wmPtr->exStyle = EX_TRANSIENT_STYLE;
  557.         parentHWND = TkWinGetHWND(wmPtr->master);
  558.      } else {
  559.         wmPtr->style = WM_TOPLEVEL_STYLE;
  560.         wmPtr->exStyle = EX_TOPLEVEL_STYLE;
  561.     }
  562.  
  563.     /*
  564.      * If the window is supposed to be visible, we need to create it
  565.      * in that state.  Otherwise we won't get the proper defaulting
  566.      * behavior for the initial position.
  567.      */
  568.  
  569.     if (wmPtr->hints.initial_state == NormalState) {
  570.         wmPtr->style |= WS_VISIBLE;
  571.     }
  572.  
  573.     /*
  574.      * Compute the border size and the location of the child window
  575.      * in the reparent window for the current window style.
  576.      */
  577.  
  578.     rect.left = rect.right = rect.top = rect.bottom = 0;
  579.     AdjustWindowRectEx(&rect, wmPtr->style, 0, wmPtr->exStyle);
  580.     wmPtr->borderWidth = rect.right - rect.left;
  581.     wmPtr->borderHeight = rect.bottom - rect.top;
  582.     wmPtr->xInParent = -rect.left;
  583.     wmPtr->yInParent = -rect.top;
  584.  
  585.     /*
  586.      * Compute the geometry of the parent and child windows.
  587.      */
  588.  
  589.     wmPtr->flags |= WM_CREATE_PENDING|WM_MOVE_PENDING;
  590.     UpdateGeometryInfo((ClientData)winPtr);
  591.     wmPtr->flags &= ~(WM_CREATE_PENDING|WM_MOVE_PENDING);
  592.  
  593.     width = wmPtr->borderWidth + winPtr->changes.width;
  594.     height = wmPtr->borderHeight + winPtr->changes.height;
  595.  
  596.     /*
  597.      * Set the initial position from the user or program specified
  598.      * location.  If nothing has been specified, then let the system
  599.      * pick a location.
  600.      */
  601.  
  602.     if (!(wmPtr->sizeHintsFlags & (USPosition | PPosition))) {
  603.         x = CW_USEDEFAULT;
  604.         y = CW_USEDEFAULT;
  605.     } else {
  606.         x = winPtr->changes.x;
  607.         y = winPtr->changes.y;
  608.     }
  609.  
  610.     /*
  611.      * Create the containing window.
  612.      */
  613.  
  614.     parentPtr = (TkWinDrawable *) ckalloc(sizeof(TkWinDrawable));
  615.     parentPtr->type = TWD_WM_WINDOW;
  616.     parentPtr->window.winPtr = winPtr;
  617.     wmPtr->reparent = (Window)parentPtr;
  618.  
  619.     createWindow = winPtr;
  620.     window  = CreateWindowEx(wmPtr->exStyle, TK_WIN_TOPLEVEL_CLASS_NAME,
  621.         wmPtr->titleUid, wmPtr->style, x, y, width, height,
  622.         parentHWND, NULL, TkWinGetAppInstance(), parentPtr);
  623.     createWindow = NULL;
  624.  
  625.     /*
  626.      * Now we need to reparent the contained window and set its
  627.      * style appropriately.  Be sure to update the style first so that
  628.      * Windows doesn't try to set the focus to the child window.
  629.      */
  630.  
  631.     SetWindowLong(child, GWL_STYLE,
  632.         WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
  633.     SetParent(child, window);
  634.  
  635.     /*
  636.      * Generate a reparent event.
  637.      */
  638.  
  639.     event.type = ReparentNotify;
  640.     winPtr->display->request++;
  641.     event.xreparent.serial = winPtr->display->request;
  642.     event.xreparent.send_event = False;
  643.     event.xreparent.display = winPtr->display;
  644.     event.xreparent.event = winPtr->window;
  645.     event.xreparent.window = winPtr->window;
  646.     event.xreparent.parent = wmPtr->reparent;
  647.     event.xreparent.x = wmPtr->xInParent;
  648.     event.xreparent.y = wmPtr->yInParent;
  649.     event.xreparent.override_redirect = winPtr->atts.override_redirect;
  650.     Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
  651.     } else if (wmPtr->hints.initial_state == WithdrawnState) {
  652.     return;
  653.     } else {
  654.     if (wmPtr->flags & WM_UPDATE_PENDING) {
  655.         Tcl_CancelIdleCall(UpdateGeometryInfo, (ClientData) winPtr);
  656.     }
  657.     UpdateGeometryInfo((ClientData) winPtr);
  658.     }
  659.  
  660.     /*
  661.      * Map the window in either the iconified or normal state.  Note that
  662.      * we only send a map event if the window is in the normal state.
  663.      */
  664.  
  665.     
  666.     if (wmPtr->hints.initial_state == IconicState) {
  667.     wmPtr->flags |= WM_SYNC_PENDING;
  668.     ShowWindow(TkWinGetHWND(wmPtr->reparent), SW_SHOWMINNOACTIVE);
  669.     wmPtr->flags &= ~WM_SYNC_PENDING;
  670.  
  671.     /*
  672.      * Send an unmap event if we are iconifying a currently displayed
  673.      * window.
  674.      */
  675.  
  676.     if (!wmPtr->withdrawn) {
  677.         XUnmapWindow(winPtr->display, winPtr->window);
  678.     }
  679.  
  680.     } else if (wmPtr->hints.initial_state == WithdrawnState) {
  681.     return;
  682.  
  683.     } else {
  684.     XMapWindow(winPtr->display, winPtr->window);
  685.  
  686.     wmPtr->flags |= WM_SYNC_PENDING;
  687.     if (IsZoomed(TkWinGetHWND(wmPtr->reparent))) {
  688.         ShowWindow(TkWinGetHWND(wmPtr->reparent), SW_SHOWMAXIMIZED);
  689.     } else {
  690.         ShowWindow(TkWinGetHWND(wmPtr->reparent), SW_SHOWNORMAL);
  691.     }
  692.     wmPtr->flags &= ~WM_SYNC_PENDING;
  693.     }
  694. }
  695.  
  696. /*
  697.  *--------------------------------------------------------------
  698.  *
  699.  * TkWmUnmapWindow --
  700.  *
  701.  *    This procedure is invoked to unmap a top-level window.  The
  702.  *    only thing it does special is unmap the decorative frame before
  703.  *    unmapping the toplevel window.
  704.  *
  705.  * Results:
  706.  *    None.
  707.  *
  708.  * Side effects:
  709.  *    Unmaps the decorative frame and the window.
  710.  *
  711.  *--------------------------------------------------------------
  712.  */
  713.  
  714. void
  715. TkWmUnmapWindow(winPtr)
  716.     TkWindow *winPtr;        /* Top-level window that's about to
  717.                  * be unmapped. */
  718. {
  719.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  720.  
  721.     wmPtr->flags |= WM_SYNC_PENDING;
  722.     ShowWindow(TkWinGetHWND(wmPtr->reparent), SW_HIDE);
  723.     wmPtr->flags &= ~WM_SYNC_PENDING;
  724.     XUnmapWindow(winPtr->display, winPtr->window);
  725. }
  726.  
  727. /*
  728.  *--------------------------------------------------------------
  729.  *
  730.  * TkWmDeadWindow --
  731.  *
  732.  *    This procedure is invoked when a top-level window is
  733.  *    about to be deleted.  It cleans up the wm-related data
  734.  *    structures for the window.
  735.  *
  736.  * Results:
  737.  *    None.
  738.  *
  739.  * Side effects:
  740.  *    The WmInfo structure for winPtr gets freed up.
  741.  *
  742.  *--------------------------------------------------------------
  743.  */
  744.  
  745. void
  746. TkWmDeadWindow(winPtr)
  747.     TkWindow *winPtr;        /* Top-level window that's being deleted. */
  748. {
  749.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  750.     WmInfo *wmPtr2;
  751.  
  752.     if (wmPtr == NULL) {
  753.     return;
  754.     }
  755.  
  756.     /*
  757.      * Clean up event related window info.
  758.      */
  759.  
  760.     if (firstWmPtr == wmPtr) {
  761.     firstWmPtr = wmPtr->nextPtr;
  762.     } else {
  763.     register WmInfo *prevPtr;
  764.  
  765.     for (prevPtr = firstWmPtr; ; prevPtr = prevPtr->nextPtr) {
  766.         if (prevPtr == NULL) {
  767.         panic("couldn't unlink window in TkWmDeadWindow");
  768.         }
  769.         if (prevPtr->nextPtr == wmPtr) {
  770.         prevPtr->nextPtr = wmPtr->nextPtr;
  771.         break;
  772.         }
  773.     }
  774.     }
  775.     if (wmPtr->hints.flags & IconPixmapHint) {
  776.     Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_pixmap);
  777.     }
  778.     if (wmPtr->hints.flags & IconMaskHint) {
  779.     Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_mask);
  780.     }
  781.     if (wmPtr->icon != NULL) {
  782.     wmPtr2 = ((TkWindow *) wmPtr->icon)->wmInfoPtr;
  783.     wmPtr2->iconFor = NULL;
  784.     wmPtr2->withdrawn = 1;
  785.     }
  786.     if (wmPtr->iconFor != NULL) {
  787.     wmPtr2 = ((TkWindow *) wmPtr->iconFor)->wmInfoPtr;
  788.     wmPtr2->icon = NULL;
  789.     wmPtr2->hints.flags &= ~IconWindowHint;
  790.     }
  791.     while (wmPtr->protPtr != NULL) {
  792.     ProtocolHandler *protPtr;
  793.  
  794.     protPtr = wmPtr->protPtr;
  795.     wmPtr->protPtr = protPtr->nextPtr;
  796.     Tcl_EventuallyFree((ClientData) protPtr, TCL_DYNAMIC);
  797.     }
  798.     if (wmPtr->cmdArgv != NULL) {
  799.     ckfree((char *) wmPtr->cmdArgv);
  800.     }
  801.     if (wmPtr->clientMachine != NULL) {
  802.     ckfree((char *) wmPtr->clientMachine);
  803.     }
  804.     if (wmPtr->flags & WM_UPDATE_PENDING) {
  805.     Tcl_CancelIdleCall(UpdateGeometryInfo, (ClientData) winPtr);
  806.     }
  807.  
  808.     /*
  809.      * Destroy the decorative frame window.  Note that the back pointer
  810.      * to the child window must be cleared before calling DestroyWindow to
  811.      * avoid generating events on a window that is already dead.
  812.      * Note that we don't free the reparent here because it will be
  813.      * freed when the WM_DESTROY message is processed.
  814.      */
  815.  
  816.     if (wmPtr->reparent != None) {
  817.     ((TkWinDrawable *) wmPtr->reparent)->window.winPtr = NULL;
  818.     DestroyWindow(TkWinGetHWND(wmPtr->reparent));
  819.     }
  820.     ckfree((char *) wmPtr);
  821.     winPtr->wmInfoPtr = NULL;
  822. }
  823.  
  824. /*
  825.  *--------------------------------------------------------------
  826.  *
  827.  * TkWmSetClass --
  828.  *
  829.  *    This procedure is invoked whenever a top-level window's
  830.  *    class is changed.  If the window has been mapped then this
  831.  *    procedure updates the window manager property for the
  832.  *    class.  If the window hasn't been mapped, the update is
  833.  *    deferred until just before the first mapping.
  834.  *
  835.  * Results:
  836.  *    None.
  837.  *
  838.  * Side effects:
  839.  *    A window property may get updated.
  840.  *
  841.  *--------------------------------------------------------------
  842.  */
  843.  
  844. void
  845. TkWmSetClass(winPtr)
  846.     TkWindow *winPtr;        /* Newly-created top-level window. */
  847. {
  848.     return;
  849. }
  850.  
  851. /*
  852.  *----------------------------------------------------------------------
  853.  *
  854.  * Tk_WmCmd --
  855.  *
  856.  *    This procedure is invoked to process the "wm" Tcl command.
  857.  *    See the user documentation for details on what it does.
  858.  *
  859.  * Results:
  860.  *    A standard Tcl result.
  861.  *
  862.  * Side effects:
  863.  *    See the user documentation.
  864.  *
  865.  *----------------------------------------------------------------------
  866.  */
  867.  
  868.     /* ARGSUSED */
  869. int
  870. Tk_WmCmd(clientData, interp, argc, argv)
  871.     ClientData clientData;    /* Main window associated with
  872.                  * interpreter. */
  873.     Tcl_Interp *interp;        /* Current interpreter. */
  874.     int argc;            /* Number of arguments. */
  875.     char **argv;        /* Argument strings. */
  876. {
  877.     Tk_Window tkwin = (Tk_Window) clientData;
  878.     TkWindow *winPtr;
  879.     register WmInfo *wmPtr;
  880.     int c;
  881.     size_t length;
  882.  
  883.     if (argc < 2) {
  884.     wrongNumArgs:
  885.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  886.         argv[0], " option window ?arg ...?\"", (char *) NULL);
  887.     return TCL_ERROR;
  888.     }
  889.     c = argv[1][0];
  890.     length = strlen(argv[1]);
  891.     if ((c == 't') && (strncmp(argv[1], "tracing", length) == 0)
  892.         && (length >= 3)) {
  893.     if ((argc != 2) && (argc != 3)) {
  894.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  895.             argv[0], " tracing ?boolean?\"", (char *) NULL);
  896.         return TCL_ERROR;
  897.     }
  898.     if (argc == 2) {
  899.         interp->result = (wmTracing) ? "on" : "off";
  900.         return TCL_OK;
  901.     }
  902.     return Tcl_GetBoolean(interp, argv[2], &wmTracing);
  903.     }
  904.  
  905.     if (argc < 3) {
  906.     goto wrongNumArgs;
  907.     }
  908.     winPtr = (TkWindow *) Tk_NameToWindow(interp, argv[2], tkwin);
  909.     if (winPtr == NULL) {
  910.     return TCL_ERROR;
  911.     }
  912.     if (!(winPtr->flags & TK_TOP_LEVEL)) {
  913.     Tcl_AppendResult(interp, "window \"", winPtr->pathName,
  914.         "\" isn't a top-level window", (char *) NULL);
  915.     return TCL_ERROR;
  916.     }
  917.     wmPtr = winPtr->wmInfoPtr;
  918.     if ((c == 'a') && (strncmp(argv[1], "aspect", length) == 0)) {
  919.     int numer1, denom1, numer2, denom2;
  920.  
  921.     if ((argc != 3) && (argc != 7)) {
  922.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  923.             argv[0], " aspect window ?minNumer minDenom ",
  924.             "maxNumer maxDenom?\"", (char *) NULL);
  925.         return TCL_ERROR;
  926.     }
  927.     if (argc == 3) {
  928.         if (wmPtr->sizeHintsFlags & PAspect) {
  929.         sprintf(interp->result, "%d %d %d %d", wmPtr->minAspect.x,
  930.             wmPtr->minAspect.y, wmPtr->maxAspect.x,
  931.             wmPtr->maxAspect.y);
  932.         }
  933.         return TCL_OK;
  934.     }
  935.     if (*argv[3] == '\0') {
  936.         wmPtr->sizeHintsFlags &= ~PAspect;
  937.     } else {
  938.         if ((Tcl_GetInt(interp, argv[3], &numer1) != TCL_OK)
  939.             || (Tcl_GetInt(interp, argv[4], &denom1) != TCL_OK)
  940.             || (Tcl_GetInt(interp, argv[5], &numer2) != TCL_OK)
  941.             || (Tcl_GetInt(interp, argv[6], &denom2) != TCL_OK)) {
  942.         return TCL_ERROR;
  943.         }
  944.         if ((numer1 <= 0) || (denom1 <= 0) || (numer2 <= 0) ||
  945.             (denom2 <= 0)) {
  946.         interp->result = "aspect number can't be <= 0";
  947.         return TCL_ERROR;
  948.         }
  949.         wmPtr->minAspect.x = numer1;
  950.         wmPtr->minAspect.y = denom1;
  951.         wmPtr->maxAspect.x = numer2;
  952.         wmPtr->maxAspect.y = denom2;
  953.         wmPtr->sizeHintsFlags |= PAspect;
  954.     }
  955.     goto updateGeom;
  956.     } else if ((c == 'c') && (strncmp(argv[1], "client", length) == 0)
  957.         && (length >= 2)) {
  958.     if ((argc != 3) && (argc != 4)) {
  959.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  960.             argv[0], " client window ?name?\"",
  961.             (char *) NULL);
  962.         return TCL_ERROR;
  963.     }
  964.     if (argc == 3) {
  965.         if (wmPtr->clientMachine != NULL) {
  966.         interp->result = wmPtr->clientMachine;
  967.         }
  968.         return TCL_OK;
  969.     }
  970.     if (argv[3][0] == 0) {
  971.         if (wmPtr->clientMachine != NULL) {
  972.         ckfree((char *) wmPtr->clientMachine);
  973.         wmPtr->clientMachine = NULL;
  974.         if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  975.             XDeleteProperty(winPtr->display, winPtr->window,
  976.                 Tk_InternAtom((Tk_Window) winPtr,
  977.                 "WM_CLIENT_MACHINE"));
  978.         }
  979.         }
  980.         return TCL_OK;
  981.     }
  982.     if (wmPtr->clientMachine != NULL) {
  983.         ckfree((char *) wmPtr->clientMachine);
  984.     }
  985.     wmPtr->clientMachine = (char *)
  986.         ckalloc((unsigned) (strlen(argv[3]) + 1));
  987.     strcpy(wmPtr->clientMachine, argv[3]);
  988.     if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  989.         XTextProperty textProp;
  990.         if (XStringListToTextProperty(&wmPtr->clientMachine, 1, &textProp)
  991.             != 0) {
  992.         XSetWMClientMachine(winPtr->display, winPtr->window,
  993.             &textProp);
  994.         XFree((char *) textProp.value);
  995.         }
  996.     }
  997.     } else if ((c == 'c') && (strncmp(argv[1], "colormapwindows", length) == 0)
  998.         && (length >= 3)) {
  999.     TkWindow **cmapList;
  1000.     TkWindow *winPtr2;
  1001.     int i, windowArgc, gotToplevel;
  1002.     char **windowArgv;
  1003.  
  1004.     if ((argc != 3) && (argc != 4)) {
  1005.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1006.             argv[0], " colormapwindows window ?windowList?\"",
  1007.             (char *) NULL);
  1008.         return TCL_ERROR;
  1009.     }
  1010.     if (argc == 3) {
  1011.         Tk_MakeWindowExist((Tk_Window) winPtr);
  1012.         for (i = 0; i < wmPtr->cmapCount; i++) {
  1013.         if ((i == (wmPtr->cmapCount-1))
  1014.             && (wmPtr->flags & WM_ADDED_TOPLEVEL_COLORMAP)) {
  1015.             break;
  1016.         }
  1017.         Tcl_AppendElement(interp, wmPtr->cmapList[i]->pathName);
  1018.         }
  1019.         return TCL_OK;
  1020.     }
  1021.     if (Tcl_SplitList(interp, argv[3], &windowArgc, &windowArgv)
  1022.         != TCL_OK) {
  1023.         return TCL_ERROR;
  1024.     }
  1025.     cmapList = (TkWindow **) ckalloc((unsigned)
  1026.         ((windowArgc+1)*sizeof(TkWindow*)));
  1027.     for (i = 0; i < windowArgc; i++) {
  1028.         winPtr2 = (TkWindow *) Tk_NameToWindow(interp, windowArgv[i],
  1029.             tkwin);
  1030.         if (winPtr2 == NULL) {
  1031.         ckfree((char *) cmapList);
  1032.         ckfree((char *) windowArgv);
  1033.         return TCL_ERROR;
  1034.         }
  1035.         if (winPtr2 == winPtr) {
  1036.         gotToplevel = 1;
  1037.         }
  1038.         if (winPtr2->window == None) {
  1039.         Tk_MakeWindowExist((Tk_Window) winPtr2);
  1040.         }
  1041.         cmapList[i] = winPtr2;
  1042.     }
  1043.     if (!gotToplevel) {
  1044.         wmPtr->flags |= WM_ADDED_TOPLEVEL_COLORMAP;
  1045.         cmapList[windowArgc] = winPtr;
  1046.         windowArgc++;
  1047.     } else {
  1048.         wmPtr->flags &= ~WM_ADDED_TOPLEVEL_COLORMAP;
  1049.     }
  1050.     wmPtr->flags |= WM_COLORMAPS_EXPLICIT;
  1051.     if (wmPtr->cmapList != NULL) {
  1052.         ckfree((char *)wmPtr->cmapList);
  1053.     }
  1054.     wmPtr->cmapList = cmapList;
  1055.     wmPtr->cmapCount = windowArgc;
  1056.     ckfree((char *) windowArgv);
  1057.  
  1058.     /*
  1059.      * Now we need to force the updated colormaps to be installed.
  1060.      */
  1061.  
  1062.     if (wmPtr == foregroundWmPtr) {
  1063.         TkWinWmInstallColormaps(TkWinGetHWND(wmPtr->reparent),
  1064.             WM_QUERYNEWPALETTE, 1);
  1065.     } else {
  1066.         TkWinWmInstallColormaps(TkWinGetHWND(wmPtr->reparent),
  1067.             WM_PALETTECHANGED, 0);
  1068.     }
  1069.     return TCL_OK;
  1070.     } else if ((c == 'c') && (strncmp(argv[1], "command", length) == 0)
  1071.         && (length >= 3)) {
  1072.     int cmdArgc;
  1073.     char **cmdArgv;
  1074.  
  1075.     if ((argc != 3) && (argc != 4)) {
  1076.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1077.             argv[0], " command window ?value?\"",
  1078.             (char *) NULL);
  1079.         return TCL_ERROR;
  1080.     }
  1081.     if (argc == 3) {
  1082.         if (wmPtr->cmdArgv != NULL) {
  1083.         interp->result = Tcl_Merge(wmPtr->cmdArgc, wmPtr->cmdArgv);
  1084.         interp->freeProc = TCL_DYNAMIC;
  1085.         }
  1086.         return TCL_OK;
  1087.     }
  1088.     if (argv[3][0] == 0) {
  1089.         if (wmPtr->cmdArgv != NULL) {
  1090.         ckfree((char *) wmPtr->cmdArgv);
  1091.         wmPtr->cmdArgv = NULL;
  1092.         if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  1093.             XDeleteProperty(winPtr->display, winPtr->window,
  1094.                 Tk_InternAtom((Tk_Window) winPtr, "WM_COMMAND"));
  1095.         }
  1096.         }
  1097.         return TCL_OK;
  1098.     }
  1099.     if (Tcl_SplitList(interp, argv[3], &cmdArgc, &cmdArgv) != TCL_OK) {
  1100.         return TCL_ERROR;
  1101.     }
  1102.     if (wmPtr->cmdArgv != NULL) {
  1103.         ckfree((char *) wmPtr->cmdArgv);
  1104.     }
  1105.     wmPtr->cmdArgc = cmdArgc;
  1106.     wmPtr->cmdArgv = cmdArgv;
  1107.     if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  1108.         XSetCommand(winPtr->display, winPtr->window, cmdArgv, cmdArgc);
  1109.     }
  1110.     } else if ((c == 'd') && (strncmp(argv[1], "deiconify", length) == 0)) {
  1111.     if (argc != 3) {
  1112.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1113.             argv[0], " deiconify window\"", (char *) NULL);
  1114.         return TCL_ERROR;
  1115.     }
  1116.     if (wmPtr->iconFor != NULL) {
  1117.         Tcl_AppendResult(interp, "can't deiconify ", argv[2],
  1118.             ": it is an icon for ", winPtr->pathName, (char *) NULL);
  1119.         return TCL_ERROR;
  1120.     }
  1121.     DeiconifyWindow(winPtr);
  1122.     } else if ((c == 'f') && (strncmp(argv[1], "focusmodel", length) == 0)
  1123.         && (length >= 2)) {
  1124.     if ((argc != 3) && (argc != 4)) {
  1125.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1126.             argv[0], " focusmodel window ?active|passive?\"",
  1127.             (char *) NULL);
  1128.         return TCL_ERROR;
  1129.     }
  1130.     if (argc == 3) {
  1131. #ifdef STk_CODE
  1132.         interp->result = wmPtr->hints.input ? "\"passive\"" : "\"active\"";
  1133. #else
  1134.         interp->result = wmPtr->hints.input ? "passive" : "active";
  1135. #endif
  1136.         return TCL_OK;
  1137.     }
  1138.     c = argv[3][0];
  1139.     length = strlen(argv[3]);
  1140.     if ((c == 'a') && (strncmp(argv[3], "active", length) == 0)) {
  1141.         wmPtr->hints.input = False;
  1142.     } else if ((c == 'p') && (strncmp(argv[3], "passive", length) == 0)) {
  1143.         wmPtr->hints.input = True;
  1144.     } else {
  1145.         Tcl_AppendResult(interp, "bad argument \"", argv[3],
  1146.             "\": must be active or passive", (char *) NULL);
  1147.         return TCL_ERROR;
  1148.     }
  1149.     } else if ((c == 'f') && (strncmp(argv[1], "frame", length) == 0)
  1150.         && (length >= 2)) {
  1151.     Window window;
  1152.  
  1153.     if (argc != 3) {
  1154.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1155.             argv[0], " frame window\"", (char *) NULL);
  1156.         return TCL_ERROR;
  1157.     }
  1158.     window = wmPtr->reparent;
  1159.     if (window == None) {
  1160.         window = Tk_WindowId((Tk_Window) winPtr);
  1161.     }
  1162. #ifdef STk_CODE
  1163.     sprintf(interp->result, "#x%x", (unsigned int) window);
  1164. #else
  1165.     sprintf(interp->result, "0x%x", (unsigned int) window);
  1166. #endif
  1167.     } else if ((c == 'g') && (strncmp(argv[1], "geometry", length) == 0)
  1168.         && (length >= 2)) {
  1169.     char xSign, ySign;
  1170.     int width, height;
  1171.  
  1172.     if ((argc != 3) && (argc != 4)) {
  1173.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1174.             argv[0], " geometry window ?newGeometry?\"",
  1175.             (char *) NULL);
  1176.         return TCL_ERROR;
  1177.     }
  1178.     if (argc == 3) {
  1179.         xSign = (wmPtr->flags & WM_NEGATIVE_X) ? '-' : '+';
  1180.         ySign = (wmPtr->flags & WM_NEGATIVE_Y) ? '-' : '+';
  1181.         if (wmPtr->gridWin != NULL) {
  1182.         width = wmPtr->reqGridWidth + (winPtr->changes.width
  1183.             - winPtr->reqWidth)/wmPtr->widthInc;
  1184.         height = wmPtr->reqGridHeight + (winPtr->changes.height
  1185.             - winPtr->reqHeight)/wmPtr->heightInc;
  1186.         } else {
  1187.         width = winPtr->changes.width;
  1188.         height = winPtr->changes.height;
  1189.         }
  1190. #ifdef STk_CODE
  1191.         sprintf(interp->result, "\"%dx%d%c%d%c%d\"", width, height,
  1192. #else
  1193.         sprintf(interp->result, "%dx%d%c%d%c%d", width, height,
  1194. #endif
  1195.             xSign, wmPtr->x, ySign, wmPtr->y);
  1196.         return TCL_OK;
  1197.     }
  1198.     if (*argv[3] == '\0') {
  1199.         wmPtr->width = -1;
  1200.         wmPtr->height = -1;
  1201.         goto updateGeom;
  1202.     }
  1203.     return ParseGeometry(interp, argv[3], winPtr);
  1204.     } else if ((c == 'g') && (strncmp(argv[1], "grid", length) == 0)
  1205.         && (length >= 3)) {
  1206.     int reqWidth, reqHeight, widthInc, heightInc;
  1207.  
  1208.     if ((argc != 3) && (argc != 7)) {
  1209.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1210.             argv[0], " grid window ?baseWidth baseHeight ",
  1211.             "widthInc heightInc?\"", (char *) NULL);
  1212.         return TCL_ERROR;
  1213.     }
  1214.     if (argc == 3) {
  1215.         if (wmPtr->sizeHintsFlags & PBaseSize) {
  1216.         sprintf(interp->result, "%d %d %d %d", wmPtr->reqGridWidth,
  1217.             wmPtr->reqGridHeight, wmPtr->widthInc,
  1218.             wmPtr->heightInc);
  1219.         }
  1220.         return TCL_OK;
  1221.     }
  1222.     if (*argv[3] == '\0') {
  1223.         /*
  1224.          * Turn off gridding and reset the width and height
  1225.          * to make sense as ungridded numbers.
  1226.          */
  1227.  
  1228.         wmPtr->sizeHintsFlags &= ~(PBaseSize|PResizeInc);
  1229.         if (wmPtr->width != -1) {
  1230.         wmPtr->width = winPtr->reqWidth + (wmPtr->width
  1231.             - wmPtr->reqGridWidth)*wmPtr->widthInc;
  1232.         wmPtr->height = winPtr->reqHeight + (wmPtr->height
  1233.             - wmPtr->reqGridHeight)*wmPtr->heightInc;
  1234.         }
  1235.         wmPtr->widthInc = 1;
  1236.         wmPtr->heightInc = 1;
  1237.     } else {
  1238.         if ((Tcl_GetInt(interp, argv[3], &reqWidth) != TCL_OK)
  1239.             || (Tcl_GetInt(interp, argv[4], &reqHeight) != TCL_OK)
  1240.             || (Tcl_GetInt(interp, argv[5], &widthInc) != TCL_OK)
  1241.             || (Tcl_GetInt(interp, argv[6], &heightInc) != TCL_OK)) {
  1242.         return TCL_ERROR;
  1243.         }
  1244.         if (reqWidth < 0) {
  1245.         interp->result = "baseWidth can't be < 0";
  1246.         return TCL_ERROR;
  1247.         }
  1248.         if (reqHeight < 0) {
  1249.         interp->result = "baseHeight can't be < 0";
  1250.         return TCL_ERROR;
  1251.         }
  1252.         if (widthInc < 0) {
  1253.         interp->result = "widthInc can't be < 0";
  1254.         return TCL_ERROR;
  1255.         }
  1256.         if (heightInc < 0) {
  1257.         interp->result = "heightInc can't be < 0";
  1258.         return TCL_ERROR;
  1259.         }
  1260.         Tk_SetGrid((Tk_Window) winPtr, reqWidth, reqHeight, widthInc,
  1261.             heightInc);
  1262.     }
  1263.     goto updateGeom;
  1264.     } else if ((c == 'g') && (strncmp(argv[1], "group", length) == 0)
  1265.         && (length >= 3)) {
  1266.     Tk_Window tkwin2;
  1267.  
  1268.     if ((argc != 3) && (argc != 4)) {
  1269.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1270.             argv[0], " group window ?pathName?\"",
  1271.             (char *) NULL);
  1272.         return TCL_ERROR;
  1273.     }
  1274.     if (argc == 3) {
  1275.         if (wmPtr->hints.flags & WindowGroupHint) {
  1276.         interp->result = wmPtr->leaderName;
  1277.         }
  1278. #ifdef STk_CODE
  1279.         STk_stringify_result(interp, interp->result);
  1280. #endif
  1281.         return TCL_OK;
  1282.     }
  1283.     if (*argv[3] == '\0') {
  1284.         wmPtr->hints.flags &= ~WindowGroupHint;
  1285.         wmPtr->leaderName = NULL;
  1286.     } else {
  1287.         tkwin2 = Tk_NameToWindow(interp, argv[3], tkwin);
  1288.         if (tkwin2 == NULL) {
  1289.         return TCL_ERROR;
  1290.         }
  1291.         Tk_MakeWindowExist(tkwin2);
  1292.         wmPtr->hints.window_group = Tk_WindowId(tkwin2);
  1293.         wmPtr->hints.flags |= WindowGroupHint;
  1294.         wmPtr->leaderName = Tk_PathName(tkwin2);
  1295.     }
  1296.     } else if ((c == 'i') && (strncmp(argv[1], "iconbitmap", length) == 0)
  1297.         && (length >= 5)) {
  1298.     Pixmap pixmap;
  1299.  
  1300.     if ((argc != 3) && (argc != 4)) {
  1301.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1302.             argv[0], " iconbitmap window ?bitmap?\"",
  1303.             (char *) NULL);
  1304.         return TCL_ERROR;
  1305.     }
  1306.     if (argc == 3) {
  1307.         if (wmPtr->hints.flags & IconPixmapHint) {
  1308.         interp->result = Tk_NameOfBitmap(winPtr->display,
  1309.             wmPtr->hints.icon_pixmap);
  1310.         }
  1311. #ifdef STk_CODE
  1312.         STk_stringify_result(interp, interp->result);
  1313. #endif
  1314.         return TCL_OK;
  1315.     }
  1316.     if (*argv[3] == '\0') {
  1317.         if (wmPtr->hints.icon_pixmap != None) {
  1318.         Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_pixmap);
  1319.         }
  1320.         wmPtr->hints.flags &= ~IconPixmapHint;
  1321.     } else {
  1322.         pixmap = Tk_GetBitmap(interp, (Tk_Window) winPtr,
  1323.             Tk_GetUid(argv[3]));
  1324.         if (pixmap == None) {
  1325.         return TCL_ERROR;
  1326.         }
  1327.         wmPtr->hints.icon_pixmap = pixmap;
  1328.         wmPtr->hints.flags |= IconPixmapHint;
  1329.     }
  1330.     } else if ((c == 'i') && (strncmp(argv[1], "iconify", length) == 0)
  1331.         && (length >= 5)) {
  1332.     if (argc != 3) {
  1333.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1334.             argv[0], " iconify window\"", (char *) NULL);
  1335.         return TCL_ERROR;
  1336.     }
  1337.     if (Tk_Attributes((Tk_Window) winPtr)->override_redirect) {
  1338.         Tcl_AppendResult(interp, "can't iconify \"", winPtr->pathName,
  1339.             "\": override-redirect flag is set", (char *) NULL);
  1340.         return TCL_ERROR;
  1341.     }
  1342.     if (wmPtr->master != None) {
  1343.         Tcl_AppendResult(interp, "can't iconify \"", winPtr->pathName,
  1344.             "\": it is a transient", (char *) NULL);
  1345.         return TCL_ERROR;
  1346.     }
  1347.     if (wmPtr->iconFor != NULL) {
  1348.         Tcl_AppendResult(interp, "can't iconify ", argv[2],
  1349.             ": it is an icon for ", winPtr->pathName, (char *) NULL);
  1350.         return TCL_ERROR;
  1351.     }
  1352.     IconifyWindow(winPtr);
  1353.     } else if ((c == 'i') && (strncmp(argv[1], "iconmask", length) == 0)
  1354.         && (length >= 5)) {
  1355.     Pixmap pixmap;
  1356.  
  1357.     if ((argc != 3) && (argc != 4)) {
  1358.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1359.             argv[0], " iconmask window ?bitmap?\"",
  1360.             (char *) NULL);
  1361.         return TCL_ERROR;
  1362.     }
  1363.     if (argc == 3) {
  1364.         if (wmPtr->hints.flags & IconMaskHint) {
  1365.         interp->result = Tk_NameOfBitmap(winPtr->display,
  1366.             wmPtr->hints.icon_mask);
  1367.         }
  1368. #ifdef STk_CODE
  1369.         STk_stringify_result(interp, interp->result);
  1370. #endif
  1371.         return TCL_OK;
  1372.     }
  1373.     if (*argv[3] == '\0') {
  1374.         if (wmPtr->hints.icon_mask != None) {
  1375.         Tk_FreeBitmap(winPtr->display, wmPtr->hints.icon_mask);
  1376.         }
  1377.         wmPtr->hints.flags &= ~IconMaskHint;
  1378.     } else {
  1379.         pixmap = Tk_GetBitmap(interp, tkwin, Tk_GetUid(argv[3]));
  1380.         if (pixmap == None) {
  1381.         return TCL_ERROR;
  1382.         }
  1383.         wmPtr->hints.icon_mask = pixmap;
  1384.         wmPtr->hints.flags |= IconMaskHint;
  1385.     }
  1386.     } else if ((c == 'i') && (strncmp(argv[1], "iconname", length) == 0)
  1387.         && (length >= 5)) {
  1388.     if (argc > 4) {
  1389.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1390.             argv[0], " iconname window ?newName?\"", (char *) NULL);
  1391.         return TCL_ERROR;
  1392.     }
  1393.     if (argc == 3) {
  1394.         interp->result = (wmPtr->iconName != NULL) ? wmPtr->iconName : "";
  1395. #ifdef STk_CODE
  1396.         STk_stringify_result(interp, interp->result);
  1397. #endif
  1398.         return TCL_OK;
  1399.     } else {
  1400.         wmPtr->iconName = Tk_GetUid(argv[3]);
  1401.         if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  1402.         XSetIconName(winPtr->display, winPtr->window, wmPtr->iconName);
  1403.         }
  1404.     }
  1405.     } else if ((c == 'i') && (strncmp(argv[1], "iconposition", length) == 0)
  1406.         && (length >= 5)) {
  1407.     int x, y;
  1408.  
  1409.     if ((argc != 3) && (argc != 5)) {
  1410.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1411.             argv[0], " iconposition window ?x y?\"",
  1412.             (char *) NULL);
  1413.         return TCL_ERROR;
  1414.     }
  1415.     if (argc == 3) {
  1416.         if (wmPtr->hints.flags & IconPositionHint) {
  1417.         sprintf(interp->result, "%d %d", wmPtr->hints.icon_x,
  1418.             wmPtr->hints.icon_y);
  1419.         }
  1420.         return TCL_OK;
  1421.     }
  1422.     if (*argv[3] == '\0') {
  1423.         wmPtr->hints.flags &= ~IconPositionHint;
  1424.     } else {
  1425.         if ((Tcl_GetInt(interp, argv[3], &x) != TCL_OK)
  1426.             || (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)){
  1427.         return TCL_ERROR;
  1428.         }
  1429.         wmPtr->hints.icon_x = x;
  1430.         wmPtr->hints.icon_y = y;
  1431.         wmPtr->hints.flags |= IconPositionHint;
  1432.     }
  1433.     } else if ((c == 'i') && (strncmp(argv[1], "iconwindow", length) == 0)
  1434.         && (length >= 5)) {
  1435.     Tk_Window tkwin2;
  1436.     WmInfo *wmPtr2;
  1437.     XSetWindowAttributes atts;
  1438.  
  1439.     if ((argc != 3) && (argc != 4)) {
  1440.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1441.             argv[0], " iconwindow window ?pathName?\"",
  1442.             (char *) NULL);
  1443.         return TCL_ERROR;
  1444.     }
  1445.     if (argc == 3) {
  1446.         if (wmPtr->icon != NULL) {
  1447.         interp->result = Tk_PathName(wmPtr->icon);
  1448.         }
  1449. #ifdef STk_CODE
  1450.         STk_stringify_result(interp, interp->result);
  1451. #endif
  1452.         return TCL_OK;
  1453.     }
  1454.     if (*argv[3] == '\0') {
  1455.         wmPtr->hints.flags &= ~IconWindowHint;
  1456.         if (wmPtr->icon != NULL) {
  1457.         /*
  1458.          * Let the window use button events again, then remove
  1459.          * it as icon window.
  1460.          */
  1461.  
  1462.         atts.event_mask = Tk_Attributes(wmPtr->icon)->event_mask
  1463.             | ButtonPressMask;
  1464.         Tk_ChangeWindowAttributes(wmPtr->icon, CWEventMask, &atts);
  1465.         wmPtr2 = ((TkWindow *) wmPtr->icon)->wmInfoPtr;
  1466.         wmPtr2->iconFor = NULL;
  1467.         wmPtr2->withdrawn = 1;
  1468.         wmPtr2->hints.initial_state = WithdrawnState;
  1469.         }
  1470.         wmPtr->icon = NULL;
  1471.     } else {
  1472.         tkwin2 = Tk_NameToWindow(interp, argv[3], tkwin);
  1473.         if (tkwin2 == NULL) {
  1474.         return TCL_ERROR;
  1475.         }
  1476.         if (!Tk_IsTopLevel(tkwin2)) {
  1477.         Tcl_AppendResult(interp, "can't use ", argv[3],
  1478.             " as icon window: not at top level", (char *) NULL);
  1479.         return TCL_ERROR;
  1480.         }
  1481.         wmPtr2 = ((TkWindow *) tkwin2)->wmInfoPtr;
  1482.         if (wmPtr2->iconFor != NULL) {
  1483.         Tcl_AppendResult(interp, argv[3], " is already an icon for ",
  1484.             Tk_PathName(wmPtr2->iconFor), (char *) NULL);
  1485.         return TCL_ERROR;
  1486.         }
  1487.         if (wmPtr->icon != NULL) {
  1488.         WmInfo *wmPtr3 = ((TkWindow *) wmPtr->icon)->wmInfoPtr;
  1489.         wmPtr3->iconFor = NULL;
  1490.         wmPtr3->withdrawn = 1;
  1491.  
  1492.         /*
  1493.          * Let the window use button events again.
  1494.          */
  1495.  
  1496.         atts.event_mask = Tk_Attributes(wmPtr->icon)->event_mask
  1497.             | ButtonPressMask;
  1498.         Tk_ChangeWindowAttributes(wmPtr->icon, CWEventMask, &atts);
  1499.         }
  1500.  
  1501.         /*
  1502.          * Disable button events in the icon window:  some window
  1503.          * managers (like olvwm) want to get the events themselves,
  1504.          * but X only allows one application at a time to receive
  1505.          * button events for a window.
  1506.          */
  1507.  
  1508.         atts.event_mask = Tk_Attributes(tkwin2)->event_mask
  1509.             & ~ButtonPressMask;
  1510.         Tk_ChangeWindowAttributes(tkwin2, CWEventMask, &atts);
  1511.         Tk_MakeWindowExist(tkwin2);
  1512.         wmPtr->hints.icon_window = Tk_WindowId(tkwin2);
  1513.         wmPtr->hints.flags |= IconWindowHint;
  1514.         wmPtr->icon = tkwin2;
  1515.         wmPtr2->iconFor = (Tk_Window) winPtr;
  1516.         if (!wmPtr2->withdrawn && !(wmPtr2->flags & WM_NEVER_MAPPED)) {
  1517.         wmPtr2->withdrawn = 0;
  1518.         if (XWithdrawWindow(Tk_Display(tkwin2), Tk_WindowId(tkwin2),
  1519.             Tk_ScreenNumber(tkwin2)) == 0) {
  1520.             interp->result =
  1521.                 "couldn't send withdraw message to window manager";
  1522.             return TCL_ERROR;
  1523.         }
  1524.         }
  1525.     }
  1526.     } else if ((c == 'm') && (strncmp(argv[1], "maxsize", length) == 0)
  1527.         && (length >= 2)) {
  1528.     int width, height;
  1529.     if ((argc != 3) && (argc != 5)) {
  1530.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1531.             argv[0], " maxsize window ?width height?\"", (char *) NULL);
  1532.         return TCL_ERROR;
  1533.     }
  1534.     if (argc == 3) {
  1535.         GetMaxSize(wmPtr, &width, &height);
  1536.         sprintf(interp->result, "%d %d", width, height);
  1537.         return TCL_OK;
  1538.     }
  1539.     if ((Tcl_GetInt(interp, argv[3], &width) != TCL_OK)
  1540.         || (Tcl_GetInt(interp, argv[4], &height) != TCL_OK)) {
  1541.         return TCL_ERROR;
  1542.     }
  1543.     wmPtr->maxWidth = width;
  1544.     wmPtr->maxHeight = height;
  1545.     goto updateGeom;
  1546.     } else if ((c == 'm') && (strncmp(argv[1], "minsize", length) == 0)
  1547.         && (length >= 2)) {
  1548.     int width, height;
  1549.     if ((argc != 3) && (argc != 5)) {
  1550.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1551.             argv[0], " minsize window ?width height?\"", (char *) NULL);
  1552.         return TCL_ERROR;
  1553.     }
  1554.     if (argc == 3) {
  1555.         GetMinSize(wmPtr, &width, &height);
  1556.         sprintf(interp->result, "%d %d", width, height);
  1557.         return TCL_OK;
  1558.     }
  1559.     if ((Tcl_GetInt(interp, argv[3], &width) != TCL_OK)
  1560.         || (Tcl_GetInt(interp, argv[4], &height) != TCL_OK)) {
  1561.         return TCL_ERROR;
  1562.     }
  1563.     wmPtr->minWidth = width;
  1564.     wmPtr->minHeight = height;
  1565.     goto updateGeom;
  1566.     } else if ((c == 'o')
  1567.         && (strncmp(argv[1], "overrideredirect", length) == 0)) {
  1568.     int boolean;
  1569.     XSetWindowAttributes atts;
  1570.  
  1571.     if ((argc != 3) && (argc != 4)) {
  1572.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1573.             argv[0], " overrideredirect window ?boolean?\"",
  1574.             (char *) NULL);
  1575.         return TCL_ERROR;
  1576.     }
  1577.     if (argc == 3) {
  1578. #ifdef STk_CODE
  1579.         interp->result = (Tk_Attributes((Tk_Window) winPtr)->override_redirect)
  1580.                           ? "#t" : "#f";
  1581. #else
  1582.         if (Tk_Attributes((Tk_Window) winPtr)->override_redirect) {
  1583.         interp->result = "1";
  1584.         } else {
  1585.         interp->result = "0";
  1586.         }
  1587. #endif
  1588.         return TCL_OK;
  1589.     }
  1590.     if (Tcl_GetBoolean(interp, argv[3], &boolean) != TCL_OK) {
  1591.         return TCL_ERROR;
  1592.     }
  1593.     atts.override_redirect = (boolean) ? True : False;
  1594.     Tk_ChangeWindowAttributes((Tk_Window) winPtr, CWOverrideRedirect,
  1595.         &atts);
  1596.     } else if ((c == 'p') && (strncmp(argv[1], "positionfrom", length) == 0)
  1597.         && (length >= 2)) {
  1598.     if ((argc != 3) && (argc != 4)) {
  1599.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1600.             argv[0], " positionfrom window ?user/program?\"",
  1601.             (char *) NULL);
  1602.         return TCL_ERROR;
  1603.     }
  1604.     if (argc == 3) {
  1605.         if (wmPtr->sizeHintsFlags & USPosition) {
  1606. #ifdef STk_CODE
  1607.         interp->result = "\"user\"";
  1608. #else
  1609.         interp->result = "user";
  1610. #endif
  1611.         } else if (wmPtr->sizeHintsFlags & PPosition) {
  1612. #ifdef STk_CODE
  1613.         interp->result = "\"program\"";
  1614. #else
  1615.         interp->result = "program";
  1616. #endif
  1617.         }
  1618.         return TCL_OK;
  1619.     }
  1620.     if (*argv[3] == '\0') {
  1621.         wmPtr->sizeHintsFlags &= ~(USPosition|PPosition);
  1622.     } else {
  1623.         c = argv[3][0];
  1624.         length = strlen(argv[3]);
  1625.         if ((c == 'u') && (strncmp(argv[3], "user", length) == 0)) {
  1626.         wmPtr->sizeHintsFlags &= ~PPosition;
  1627.         wmPtr->sizeHintsFlags |= USPosition;
  1628.         } else if ((c == 'p') && (strncmp(argv[3], "program", length) == 0)) {
  1629.         wmPtr->sizeHintsFlags &= ~USPosition;
  1630.         wmPtr->sizeHintsFlags |= PPosition;
  1631.         } else {
  1632.         Tcl_AppendResult(interp, "bad argument \"", argv[3],
  1633.             "\": must be program or user", (char *) NULL);
  1634.         return TCL_ERROR;
  1635.         }
  1636.     }
  1637.     goto updateGeom;
  1638.     } else if ((c == 'p') && (strncmp(argv[1], "protocol", length) == 0)
  1639.         && (length >= 2)) {
  1640.     register ProtocolHandler *protPtr, *prevPtr;
  1641.     Atom protocol;
  1642.     int cmdLength;
  1643.  
  1644.     if ((argc < 3) || (argc > 5)) {
  1645.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1646.             argv[0], " protocol window ?name? ?command?\"",
  1647.             (char *) NULL);
  1648.         return TCL_ERROR;
  1649.     }
  1650.     if (argc == 3) {
  1651.         /*
  1652.          * Return a list of all defined protocols for the window.
  1653.          */
  1654.         for (protPtr = wmPtr->protPtr; protPtr != NULL;
  1655.             protPtr = protPtr->nextPtr) {
  1656.         Tcl_AppendElement(interp,
  1657.             Tk_GetAtomName((Tk_Window) winPtr, protPtr->protocol));
  1658.         }
  1659.         return TCL_OK;
  1660.     }
  1661.     protocol = Tk_InternAtom((Tk_Window) winPtr, argv[3]);
  1662.     if (argc == 4) {
  1663.         /*
  1664.          * Return the command to handle a given protocol.
  1665.          */
  1666.         for (protPtr = wmPtr->protPtr; protPtr != NULL;
  1667.             protPtr = protPtr->nextPtr) {
  1668.         if (protPtr->protocol == protocol) {
  1669.             interp->result = protPtr->command;
  1670.             return TCL_OK;
  1671.         }
  1672.         }
  1673.         return TCL_OK;
  1674.     }
  1675.  
  1676.     /*
  1677.      * Delete any current protocol handler, then create a new
  1678.      * one with the specified command, unless the command is
  1679.      * empty.
  1680.      */
  1681.  
  1682.     for (protPtr = wmPtr->protPtr, prevPtr = NULL; protPtr != NULL;
  1683.         prevPtr = protPtr, protPtr = protPtr->nextPtr) {
  1684.         if (protPtr->protocol == protocol) {
  1685.         if (prevPtr == NULL) {
  1686.             wmPtr->protPtr = protPtr->nextPtr;
  1687.         } else {
  1688.             prevPtr->nextPtr = protPtr->nextPtr;
  1689.         }
  1690.         Tcl_EventuallyFree((ClientData) protPtr, TCL_DYNAMIC);
  1691.         break;
  1692.         }
  1693.     }
  1694.     cmdLength = strlen(argv[4]);
  1695.     if (cmdLength > 0) {
  1696.         protPtr = (ProtocolHandler *) ckalloc(HANDLER_SIZE(cmdLength));
  1697.         protPtr->protocol = protocol;
  1698.         protPtr->nextPtr = wmPtr->protPtr;
  1699.         wmPtr->protPtr = protPtr;
  1700.         protPtr->interp = interp;
  1701.         strcpy(protPtr->command, argv[4]);
  1702.     }
  1703.     } else if ((c == 'r') && (strncmp(argv[1], "resizable", length) == 0)) {
  1704.     int width, height;
  1705.  
  1706.     if ((argc != 3) && (argc != 5)) {
  1707.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1708.             argv[0], " resizable window ?width height?\"",
  1709.             (char *) NULL);
  1710.         return TCL_ERROR;
  1711.     }
  1712.     if (argc == 3) {
  1713.         sprintf(interp->result, "%d %d",
  1714.             (wmPtr->flags  & WM_WIDTH_NOT_RESIZABLE) ? 0 : 1,
  1715.             (wmPtr->flags  & WM_HEIGHT_NOT_RESIZABLE) ? 0 : 1);
  1716.         return TCL_OK;
  1717.     }
  1718.     if ((Tcl_GetBoolean(interp, argv[3], &width) != TCL_OK)
  1719.         || (Tcl_GetBoolean(interp, argv[4], &height) != TCL_OK)) {
  1720.         return TCL_ERROR;
  1721.     }
  1722.     if (width) {
  1723.         wmPtr->flags &= ~WM_WIDTH_NOT_RESIZABLE;
  1724.     } else {
  1725.         wmPtr->flags |= WM_WIDTH_NOT_RESIZABLE;
  1726.     }
  1727.     if (height) {
  1728.         wmPtr->flags &= ~WM_HEIGHT_NOT_RESIZABLE;
  1729.     } else {
  1730.         wmPtr->flags |= WM_HEIGHT_NOT_RESIZABLE;
  1731.     }
  1732.     wmPtr->flags |= WM_UPDATE_SIZE_HINTS;
  1733.     goto updateGeom;
  1734.     } else if ((c == 's') && (strncmp(argv[1], "sizefrom", length) == 0)
  1735.         && (length >= 2)) {
  1736.     if ((argc != 3) && (argc != 4)) {
  1737.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1738.             argv[0], " sizefrom window ?user|program?\"",
  1739.             (char *) NULL);
  1740.         return TCL_ERROR;
  1741.     }
  1742.     if (argc == 3) {
  1743.         if (wmPtr->sizeHintsFlags & USSize) {
  1744. #ifdef STk_CODE
  1745.         interp->result = "\"user\"";
  1746. #else
  1747.         interp->result = "user";
  1748. #endif
  1749.         } else if (wmPtr->sizeHintsFlags & PSize) {
  1750. #ifdef STk_CODE
  1751.         interp->result = "\"program\"";
  1752. #else
  1753.         interp->result = "program";
  1754. #endif
  1755.         }
  1756.         return TCL_OK;
  1757.     }
  1758.     if (*argv[3] == '\0') {
  1759.         wmPtr->sizeHintsFlags &= ~(USSize|PSize);
  1760.     } else {
  1761.         c = argv[3][0];
  1762.         length = strlen(argv[3]);
  1763.         if ((c == 'u') && (strncmp(argv[3], "user", length) == 0)) {
  1764.         wmPtr->sizeHintsFlags &= ~PSize;
  1765.         wmPtr->sizeHintsFlags |= USSize;
  1766.         } else if ((c == 'p')
  1767.             && (strncmp(argv[3], "program", length) == 0)) {
  1768.         wmPtr->sizeHintsFlags &= ~USSize;
  1769.         wmPtr->sizeHintsFlags |= PSize;
  1770.         } else {
  1771.         Tcl_AppendResult(interp, "bad argument \"", argv[3],
  1772.             "\": must be program or user", (char *) NULL);
  1773.         return TCL_ERROR;
  1774.         }
  1775.     }
  1776.     goto updateGeom;
  1777.     } else if ((c == 's') && (strncmp(argv[1], "state", length) == 0)
  1778.         && (length >= 2)) {
  1779.     if (argc != 3) {
  1780.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1781.             argv[0], " state window\"", (char *) NULL);
  1782.         return TCL_ERROR;
  1783.     }
  1784.     if (wmPtr->iconFor != NULL) {
  1785.         interp->result = "icon";
  1786.     } else if (wmPtr->withdrawn) {
  1787.         interp->result = "withdrawn";
  1788.     } else if (Tk_IsMapped((Tk_Window) winPtr)
  1789.         || ((wmPtr->flags & WM_NEVER_MAPPED)
  1790.         && (wmPtr->hints.initial_state == NormalState))) {
  1791. #ifdef STk_CODE
  1792.         interp->result = "\"normal\"";
  1793. #else
  1794.         interp->result = "normal";
  1795. #endif
  1796.     } else {
  1797. #ifdef STk_CODE
  1798.         interp->result = "\"iconic\"";
  1799. #else
  1800.         interp->result = "iconic";
  1801. #endif
  1802.     }
  1803.     } else if ((c == 't') && (strncmp(argv[1], "title", length) == 0)
  1804.         && (length >= 2)) {
  1805.     if (argc > 4) {
  1806.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1807.             argv[0], " title window ?newTitle?\"", (char *) NULL);
  1808.         return TCL_ERROR;
  1809.     }
  1810.     if (argc == 3) {
  1811. #ifdef STk_CODE
  1812.         STk_stringify_result(interp, (wmPtr->titleUid != NULL) ? wmPtr->titleUid
  1813.                          : winPtr->nameUid);
  1814. #else
  1815.         interp->result = (wmPtr->titleUid != NULL) ? wmPtr->titleUid
  1816.             : winPtr->nameUid;
  1817. #endif
  1818.         return TCL_OK;
  1819.     } else {
  1820.         wmPtr->titleUid = Tk_GetUid(argv[3]);
  1821.         if (!(wmPtr->flags & WM_NEVER_MAPPED) && wmPtr->reparent != None) {
  1822.         SetWindowText(TkWinGetHWND(wmPtr->reparent), wmPtr->titleUid);
  1823.         }
  1824.     }
  1825.     } else if ((c == 't') && (strncmp(argv[1], "transient", length) == 0)
  1826.         && (length >= 3)) {
  1827.     Tk_Window master;
  1828.  
  1829.     if ((argc != 3) && (argc != 4)) {
  1830.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1831.             argv[0], " transient window ?master?\"", (char *) NULL);
  1832.         return TCL_ERROR;
  1833.     }
  1834.     if (argc == 3) {
  1835.         if (wmPtr->master != None) {
  1836.         interp->result = wmPtr->masterWindowName;
  1837.         }
  1838.         return TCL_OK;
  1839.     }
  1840.     if (argv[3][0] == '\0') {
  1841.         wmPtr->master = None;
  1842.         wmPtr->masterWindowName = NULL;
  1843.     } else {
  1844.         master = Tk_NameToWindow(interp, argv[3], tkwin);
  1845.         if (master == NULL) {
  1846.         return TCL_ERROR;
  1847.         }
  1848.         Tk_MakeWindowExist(master);
  1849.         wmPtr->master = Tk_WindowId(master);
  1850.         wmPtr->masterWindowName = Tk_PathName(master);
  1851.     }
  1852.     if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  1853.         XSetTransientForHint(winPtr->display, winPtr->window,
  1854.             wmPtr->master);
  1855.     }
  1856.     } else if ((c == 'w') && (strncmp(argv[1], "withdraw", length) == 0)) {
  1857.     if (argc != 3) {
  1858.         Tcl_AppendResult(interp, "wrong # arguments: must be \"",
  1859.             argv[0], " withdraw window\"", (char *) NULL);
  1860.         return TCL_ERROR;
  1861.     }
  1862.     if (wmPtr->iconFor != NULL) {
  1863.         Tcl_AppendResult(interp, "can't withdraw ", argv[2],
  1864.             ": it is an icon for ", Tk_PathName(wmPtr->iconFor),
  1865.             (char *) NULL);
  1866.         return TCL_ERROR;
  1867.     }
  1868.     wmPtr->hints.initial_state = WithdrawnState;
  1869.     wmPtr->withdrawn = 1;
  1870.     if (wmPtr->flags & WM_NEVER_MAPPED) {
  1871.         return TCL_OK;
  1872.     }
  1873.  
  1874.     TkWmUnmapWindow(winPtr);
  1875.     } else {
  1876.     Tcl_AppendResult(interp, "unknown or ambiguous option \"", argv[1],
  1877.         "\": must be aspect, client, command, deiconify, ",
  1878.         "focusmodel, frame, geometry, grid, group, iconbitmap, ",
  1879.         "iconify, iconmask, iconname, iconposition, ",
  1880.         "iconwindow, maxsize, minsize, overrideredirect, ",
  1881.         "positionfrom, protocol, resizable, sizefrom, state, title, ",
  1882.         "tracing, transient, or withdraw",
  1883.         (char *) NULL);
  1884.     return TCL_ERROR;
  1885.     }
  1886.     return TCL_OK;
  1887.  
  1888.     updateGeom:
  1889.     if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
  1890.     Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
  1891.     wmPtr->flags |= WM_UPDATE_PENDING;
  1892.     }
  1893.     return TCL_OK;
  1894. }
  1895.  
  1896. /*
  1897.  *----------------------------------------------------------------------
  1898.  *
  1899.  * Tk_SetGrid --
  1900.  *
  1901.  *    This procedure is invoked by a widget when it wishes to set a grid
  1902.  *    coordinate system that controls the size of a top-level window.
  1903.  *    It provides a C interface equivalent to the "wm grid" command and
  1904.  *    is usually asscoiated with the -setgrid option.
  1905.  *
  1906.  * Results:
  1907.  *    None.
  1908.  *
  1909.  * Side effects:
  1910.  *    Grid-related information will be passed to the window manager, so
  1911.  *    that the top-level window associated with tkwin will resize on
  1912.  *    even grid units.  If some other window already controls gridding
  1913.  *    for the top-level window then this procedure call has no effect.
  1914.  *
  1915.  *----------------------------------------------------------------------
  1916.  */
  1917.  
  1918. void
  1919. Tk_SetGrid(tkwin, reqWidth, reqHeight, widthInc, heightInc)
  1920.     Tk_Window tkwin;        /* Token for window.  New window mgr info
  1921.                  * will be posted for the top-level window
  1922.                  * associated with this window. */
  1923.     int reqWidth;        /* Width (in grid units) corresponding to
  1924.                  * the requested geometry for tkwin. */
  1925.     int reqHeight;        /* Height (in grid units) corresponding to
  1926.                  * the requested geometry for tkwin. */
  1927.     int widthInc, heightInc;    /* Pixel increments corresponding to a
  1928.                  * change of one grid unit. */
  1929. {
  1930.     TkWindow *winPtr = (TkWindow *) tkwin;
  1931.     register WmInfo *wmPtr;
  1932.  
  1933.     /*
  1934.      * Find the top-level window for tkwin, plus the window manager
  1935.      * information.
  1936.      */
  1937.  
  1938.     while (!(winPtr->flags & TK_TOP_LEVEL)) {
  1939.     winPtr = winPtr->parentPtr;
  1940.     }
  1941.     wmPtr = winPtr->wmInfoPtr;
  1942.  
  1943.     if ((wmPtr->gridWin != NULL) && (wmPtr->gridWin != tkwin)) {
  1944.     return;
  1945.     }
  1946.  
  1947.     if ((wmPtr->reqGridWidth == reqWidth)
  1948.         && (wmPtr->reqGridHeight == reqHeight)
  1949.         && (wmPtr->widthInc == widthInc)
  1950.         && (wmPtr->heightInc == heightInc)
  1951.         && ((wmPtr->sizeHintsFlags & (PBaseSize|PResizeInc))
  1952.             == PBaseSize|PResizeInc)) {
  1953.     return;
  1954.     }
  1955.  
  1956.     /*
  1957.      * If gridding was previously off, then forget about any window
  1958.      * size requests made by the user or via "wm geometry":  these are
  1959.      * in pixel units and there's no easy way to translate them to
  1960.      * grid units since the new requested size of the top-level window in
  1961.      * pixels may not yet have been registered yet (it may filter up
  1962.      * the hierarchy in DoWhenIdle handlers).  However, if the window
  1963.      * has never been mapped yet then just leave the window size alone:
  1964.      * assume that it is intended to be in grid units but just happened
  1965.      * to have been specified before this procedure was called.
  1966.      */
  1967.  
  1968.     if ((wmPtr->gridWin == NULL) && !(wmPtr->flags & WM_NEVER_MAPPED)) {
  1969.     wmPtr->width = -1;
  1970.     wmPtr->height = -1;
  1971.     }
  1972.  
  1973.     /* 
  1974.      * Set the new gridding information, and start the process of passing
  1975.      * all of this information to the window manager.
  1976.      */
  1977.  
  1978.     wmPtr->gridWin = tkwin;
  1979.     wmPtr->reqGridWidth = reqWidth;
  1980.     wmPtr->reqGridHeight = reqHeight;
  1981.     wmPtr->widthInc = widthInc;
  1982.     wmPtr->heightInc = heightInc;
  1983.     wmPtr->sizeHintsFlags |= PBaseSize|PResizeInc;
  1984.     if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
  1985.     Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
  1986.     wmPtr->flags |= WM_UPDATE_PENDING;
  1987.     }
  1988. }
  1989.  
  1990. /*
  1991.  *----------------------------------------------------------------------
  1992.  *
  1993.  * Tk_UnsetGrid --
  1994.  *
  1995.  *    This procedure cancels the effect of a previous call
  1996.  *    to Tk_SetGrid.
  1997.  *
  1998.  * Results:
  1999.  *    None.
  2000.  *
  2001.  * Side effects:
  2002.  *    If tkwin currently controls gridding for its top-level window,
  2003.  *    gridding is cancelled for that top-level window;  if some other
  2004.  *    window controls gridding then this procedure has no effect.
  2005.  *
  2006.  *----------------------------------------------------------------------
  2007.  */
  2008.  
  2009. void
  2010. Tk_UnsetGrid(tkwin)
  2011.     Tk_Window tkwin;        /* Token for window that is currently
  2012.                  * controlling gridding. */
  2013. {
  2014.     TkWindow *winPtr = (TkWindow *) tkwin;
  2015.     register WmInfo *wmPtr;
  2016.  
  2017.     /*
  2018.      * Find the top-level window for tkwin, plus the window manager
  2019.      * information.
  2020.      */
  2021.  
  2022.     while (!(winPtr->flags & TK_TOP_LEVEL)) {
  2023.     winPtr = winPtr->parentPtr;
  2024.     }
  2025.     wmPtr = winPtr->wmInfoPtr;
  2026.     if (tkwin != wmPtr->gridWin) {
  2027.     return;
  2028.     }
  2029.  
  2030.     wmPtr->gridWin = NULL;
  2031.     wmPtr->sizeHintsFlags &= ~(PBaseSize|PResizeInc);
  2032.     if (wmPtr->width != -1) {
  2033.     wmPtr->width = winPtr->reqWidth + (wmPtr->width
  2034.         - wmPtr->reqGridWidth)*wmPtr->widthInc;
  2035.     wmPtr->height = winPtr->reqHeight + (wmPtr->height
  2036.         - wmPtr->reqGridHeight)*wmPtr->heightInc;
  2037.     }
  2038.     wmPtr->widthInc = 1;
  2039.     wmPtr->heightInc = 1;
  2040.  
  2041.     if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
  2042.     Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
  2043.     wmPtr->flags |= WM_UPDATE_PENDING;
  2044.     }
  2045. }
  2046.  
  2047. /*
  2048.  *----------------------------------------------------------------------
  2049.  *
  2050.  * TopLevelEventProc --
  2051.  *
  2052.  *    This procedure is invoked when a top-level (or other externally-
  2053.  *    managed window) is restructured in any way.
  2054.  *
  2055.  * Results:
  2056.  *    None.
  2057.  *
  2058.  * Side effects:
  2059.  *    Tk's internal data structures for the window get modified to
  2060.  *    reflect the structural change.
  2061.  *
  2062.  *----------------------------------------------------------------------
  2063.  */
  2064.  
  2065. static void
  2066. TopLevelEventProc(clientData, eventPtr)
  2067.     ClientData clientData;        /* Window for which event occurred. */
  2068.     XEvent *eventPtr;            /* Event that just happened. */
  2069. {
  2070.     register TkWindow *winPtr = (TkWindow *) clientData;
  2071.  
  2072.     if (eventPtr->type == DestroyNotify) {
  2073.     Tk_ErrorHandler handler;
  2074.  
  2075.     if (!(winPtr->flags & TK_ALREADY_DEAD)) {
  2076.         /*
  2077.          * A top-level window was deleted externally (e.g., by the window
  2078.          * manager).  This is probably not a good thing, but cleanup as
  2079.          * best we can.  The error handler is needed because
  2080.          * Tk_DestroyWindow will try to destroy the window, but of course
  2081.          * it's already gone.
  2082.          */
  2083.     
  2084.         handler = Tk_CreateErrorHandler(winPtr->display, -1, -1, -1,
  2085.             (Tk_ErrorProc *) NULL, (ClientData) NULL);
  2086.         Tk_DestroyWindow((Tk_Window) winPtr);
  2087.         Tk_DeleteErrorHandler(handler);
  2088.     }
  2089.     }
  2090. }
  2091.  
  2092. /*
  2093.  *----------------------------------------------------------------------
  2094.  *
  2095.  * TopLevelReqProc --
  2096.  *
  2097.  *    This procedure is invoked by the geometry manager whenever
  2098.  *    the requested size for a top-level window is changed.
  2099.  *
  2100.  * Results:
  2101.  *    None.
  2102.  *
  2103.  * Side effects:
  2104.  *    Arrange for the window to be resized to satisfy the request
  2105.  *    (this happens as a when-idle action).
  2106.  *
  2107.  *----------------------------------------------------------------------
  2108.  */
  2109.  
  2110.     /* ARGSUSED */
  2111. static void
  2112. TopLevelReqProc(dummy, tkwin)
  2113.     ClientData dummy;            /* Not used. */
  2114.     Tk_Window tkwin;            /* Information about window. */
  2115. {
  2116.     TkWindow *winPtr = (TkWindow *) tkwin;
  2117.     WmInfo *wmPtr;
  2118.  
  2119.     wmPtr = winPtr->wmInfoPtr;
  2120.     if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
  2121.     Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
  2122.     wmPtr->flags |= WM_UPDATE_PENDING;
  2123.     }
  2124. }
  2125.  
  2126. /*
  2127.  *----------------------------------------------------------------------
  2128.  *
  2129.  * UpdateGeometryInfo --
  2130.  *
  2131.  *    This procedure is invoked when a top-level window is first
  2132.  *    mapped, and also as a when-idle procedure, to bring the
  2133.  *    geometry and/or position of a top-level window back into
  2134.  *    line with what has been requested by the user and/or widgets.
  2135.  *    This procedure doesn't return until the window manager has
  2136.  *    responded to the geometry change.
  2137.  *
  2138.  * Results:
  2139.  *    None.
  2140.  *
  2141.  * Side effects:
  2142.  *    The window's size and location may change, unless the WM prevents
  2143.  *    that from happening.
  2144.  *
  2145.  *----------------------------------------------------------------------
  2146.  */
  2147.  
  2148. static void
  2149. UpdateGeometryInfo(clientData)
  2150.     ClientData clientData;        /* Pointer to the window's record. */
  2151. {
  2152.     register TkWindow *winPtr = (TkWindow *) clientData;
  2153.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  2154.     int x, y, width, height;
  2155.  
  2156.     wmPtr->flags &= ~WM_UPDATE_PENDING;
  2157.  
  2158.     /*
  2159.      * Compute the new size for the top-level window.  See the
  2160.      * user documentation for details on this, but the size
  2161.      * requested depends on (a) the size requested internally
  2162.      * by the window's widgets, (b) the size requested by the
  2163.      * user in a "wm geometry" command or via wm-based interactive
  2164.      * resizing (if any), and (c) whether or not the window is
  2165.      * gridded.  Don't permit sizes <= 0 because this upsets
  2166.      * the X server.
  2167.      */
  2168.  
  2169.     if (wmPtr->width == -1) {
  2170.     width = winPtr->reqWidth;
  2171.     } else if (wmPtr->gridWin != NULL) {
  2172.     width = winPtr->reqWidth
  2173.         + (wmPtr->width - wmPtr->reqGridWidth)*wmPtr->widthInc;
  2174.     } else {
  2175.     width = wmPtr->width;
  2176.     }
  2177.     if (width <= 0) {
  2178.     width = 1;
  2179.     }
  2180.     if (wmPtr->height == -1) {
  2181.     height = winPtr->reqHeight;
  2182.     } else if (wmPtr->gridWin != NULL) {
  2183.     height = winPtr->reqHeight
  2184.         + (wmPtr->height - wmPtr->reqGridHeight)*wmPtr->heightInc;
  2185.     } else {
  2186.     height = wmPtr->height;
  2187.     }
  2188.     if (height <= 0) {
  2189.     height = 1;
  2190.     }
  2191.  
  2192.     /*
  2193.      * Compute the new position for the upper-left pixel of the window's
  2194.      * decorative frame.  This is tricky, because we need to include the
  2195.      * border widths supplied by a reparented parent in this calculation,
  2196.      * but can't use the parent's current overall size since that may
  2197.      * change as a result of this code.
  2198.      */
  2199.  
  2200.     if (wmPtr->flags & WM_NEGATIVE_X) {
  2201.     x = DisplayWidth(winPtr->display, winPtr->screenNum) - wmPtr->x
  2202.         - (width + wmPtr->borderWidth);
  2203.     } else {
  2204.     x =  wmPtr->x;
  2205.     }
  2206.     if (wmPtr->flags & WM_NEGATIVE_Y) {
  2207.     y = DisplayHeight(winPtr->display, winPtr->screenNum) - wmPtr->y
  2208.         - (height + wmPtr->borderHeight);
  2209.     } else {
  2210.     y =  wmPtr->y;
  2211.     }
  2212.  
  2213.     /*
  2214.      * Reconfigure the window if it isn't already configured correctly.  Base
  2215.      * the size check on what we *asked for* last time, not what we got.
  2216.      */
  2217.  
  2218.     if ((wmPtr->flags & WM_MOVE_PENDING)
  2219.         || (width != wmPtr->configWidth)
  2220.         || (height != wmPtr->configHeight)) {
  2221.     wmPtr->configWidth = width;
  2222.     wmPtr->configHeight = height;
  2223.         
  2224.     /*
  2225.      * Don't bother moving the window if we are in the process of
  2226.      * creating it.  Just update the geometry info based on what
  2227.      * we asked for.
  2228.      */
  2229.  
  2230.     if (!(wmPtr->flags & WM_CREATE_PENDING)) {
  2231.         wmPtr->flags |= WM_SYNC_PENDING;
  2232.         MoveWindow(TkWinGetHWND(wmPtr->reparent), x, y,
  2233.             width + wmPtr->borderWidth,
  2234.             height + wmPtr->borderHeight, TRUE);
  2235.         wmPtr->flags &= ~WM_SYNC_PENDING;
  2236.     } else {
  2237.         winPtr->changes.x = x;
  2238.         winPtr->changes.y = y;
  2239.         winPtr->changes.width = width;
  2240.         winPtr->changes.height = height;
  2241.     }
  2242.     } else {
  2243.     return;
  2244.     }
  2245. }
  2246.  
  2247. /*
  2248.  *--------------------------------------------------------------
  2249.  *
  2250.  * ParseGeometry --
  2251.  *
  2252.  *    This procedure parses a geometry string and updates
  2253.  *    information used to control the geometry of a top-level
  2254.  *    window.
  2255.  *
  2256.  * Results:
  2257.  *    A standard Tcl return value, plus an error message in
  2258.  *    interp->result if an error occurs.
  2259.  *
  2260.  * Side effects:
  2261.  *    The size and/or location of winPtr may change.
  2262.  *
  2263.  *--------------------------------------------------------------
  2264.  */
  2265.  
  2266. static int
  2267. ParseGeometry(interp, string, winPtr)
  2268.     Tcl_Interp *interp;        /* Used for error reporting. */
  2269.     char *string;        /* String containing new geometry.  Has the
  2270.                  * standard form "=wxh+x+y". */
  2271.     TkWindow *winPtr;        /* Pointer to top-level window whose
  2272.                  * geometry is to be changed. */
  2273. {
  2274.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  2275.     int x, y, width, height, flags;
  2276.     char *end;
  2277.     register char *p = string;
  2278.  
  2279.     /*
  2280.      * The leading "=" is optional.
  2281.      */
  2282.  
  2283.     if (*p == '=') {
  2284.     p++;
  2285.     }
  2286.  
  2287.     /*
  2288.      * Parse the width and height, if they are present.  Don't
  2289.      * actually update any of the fields of wmPtr until we've
  2290.      * successfully parsed the entire geometry string.
  2291.      */
  2292.  
  2293.     width = wmPtr->width;
  2294.     height = wmPtr->height;
  2295.     x = wmPtr->x;
  2296.     y = wmPtr->y;
  2297.     flags = wmPtr->flags;
  2298.     if (isdigit(UCHAR(*p))) {
  2299.     width = strtoul(p, &end, 10);
  2300.     p = end;
  2301.     if (*p != 'x') {
  2302.         goto error;
  2303.     }
  2304.     p++;
  2305.     if (!isdigit(UCHAR(*p))) {
  2306.         goto error;
  2307.     }
  2308.     height = strtoul(p, &end, 10);
  2309.     p = end;
  2310.     }
  2311.  
  2312.     /*
  2313.      * Parse the X and Y coordinates, if they are present.
  2314.      */
  2315.  
  2316.     if (*p != '\0') {
  2317.     flags &= ~(WM_NEGATIVE_X | WM_NEGATIVE_Y);
  2318.     if (*p == '-') {
  2319.         flags |= WM_NEGATIVE_X;
  2320.     } else if (*p != '+') {
  2321.         goto error;
  2322.     }
  2323.     x = strtol(p+1, &end, 10);
  2324.     p = end;
  2325.     if (*p == '-') {
  2326.         flags |= WM_NEGATIVE_Y;
  2327.     } else if (*p != '+') {
  2328.         goto error;
  2329.     }
  2330.     y = strtol(p+1, &end, 10);
  2331.     if (*end != '\0') {
  2332.         goto error;
  2333.     }
  2334.  
  2335.     /*
  2336.      * Assume that the geometry information came from the user,
  2337.      * unless an explicit source has been specified.  Otherwise
  2338.      * most window managers assume that the size hints were
  2339.      * program-specified and they ignore them.
  2340.      */
  2341.  
  2342.     if ((wmPtr->sizeHintsFlags & (USPosition|PPosition)) == 0) {
  2343.         wmPtr->sizeHintsFlags |= USPosition;
  2344.     }
  2345.     }
  2346.  
  2347.     /*
  2348.      * Everything was parsed OK.  Update the fields of *wmPtr and
  2349.      * arrange for the appropriate information to be percolated out
  2350.      * to the window manager at the next idle moment.
  2351.      */
  2352.  
  2353.     wmPtr->width = width;
  2354.     wmPtr->height = height;
  2355.     if ((x != wmPtr->x) || (y != wmPtr->y)
  2356.         || ((flags & (WM_NEGATIVE_X|WM_NEGATIVE_Y))
  2357.         != (wmPtr->flags & (WM_NEGATIVE_X|WM_NEGATIVE_Y)))) {
  2358.     wmPtr->x = x;
  2359.     wmPtr->y = y;
  2360.     flags |= WM_MOVE_PENDING;
  2361.     }
  2362.     wmPtr->flags = flags;
  2363.  
  2364.     if (!(wmPtr->flags & (WM_UPDATE_PENDING|WM_NEVER_MAPPED))) {
  2365.     Tcl_DoWhenIdle(UpdateGeometryInfo, (ClientData) winPtr);
  2366.     wmPtr->flags |= WM_UPDATE_PENDING;
  2367.     }
  2368.     return TCL_OK;
  2369.  
  2370.     error:
  2371.     Tcl_AppendResult(interp, "bad geometry specifier \"",
  2372.         string, "\"", (char *) NULL);
  2373.     return TCL_ERROR;
  2374. }
  2375.  
  2376. /*
  2377.  *----------------------------------------------------------------------
  2378.  *
  2379.  * Tk_GetRootCoords --
  2380.  *
  2381.  *    Given a token for a window, this procedure traces through the
  2382.  *    window's lineage to find the (virtual) root-window coordinates
  2383.  *    corresponding to point (0,0) in the window.
  2384.  *
  2385.  * Results:
  2386.  *    The locations pointed to by xPtr and yPtr are filled in with
  2387.  *    the root coordinates of the (0,0) point in tkwin.  If a virtual
  2388.  *    root window is in effect for the window, then the coordinates
  2389.  *    in the virtual root are returned.
  2390.  *
  2391.  * Side effects:
  2392.  *    None.
  2393.  *
  2394.  *----------------------------------------------------------------------
  2395.  */
  2396.  
  2397. void
  2398. Tk_GetRootCoords(tkwin, xPtr, yPtr)
  2399.     Tk_Window tkwin;        /* Token for window. */
  2400.     int *xPtr;            /* Where to store x-displacement of (0,0). */
  2401.     int *yPtr;            /* Where to store y-displacement of (0,0). */
  2402. {
  2403.     int x, y;
  2404.     register TkWindow *winPtr = (TkWindow *) tkwin;
  2405.  
  2406.     /*
  2407.      * Search back through this window's parents all the way to a
  2408.      * top-level window, combining the offsets of each window within
  2409.      * its parent.
  2410.      */
  2411.  
  2412.     x = y = 0;
  2413.     while (1) {
  2414.     x += winPtr->changes.x + winPtr->changes.border_width;
  2415.     y += winPtr->changes.y + winPtr->changes.border_width;
  2416.     if (winPtr->flags & TK_TOP_LEVEL) {
  2417.         x += winPtr->wmInfoPtr->xInParent;
  2418.         y += winPtr->wmInfoPtr->yInParent;
  2419.         break;
  2420.     }
  2421.     winPtr = winPtr->parentPtr;
  2422.     }
  2423.     *xPtr = x;
  2424.     *yPtr = y;
  2425. }
  2426.  
  2427. /*
  2428.  *----------------------------------------------------------------------
  2429.  *
  2430.  * Tk_CoordsToWindow --
  2431.  *
  2432.  *    Given the (virtual) root coordinates of a point, this procedure
  2433.  *    returns the token for the top-most window covering that point,
  2434.  *    if there exists such a window in this application.
  2435.  *
  2436.  * Results:
  2437.  *    The return result is either a token for the window corresponding
  2438.  *    to rootX and rootY, or else NULL to indicate that there is no such
  2439.  *    window.
  2440.  *
  2441.  * Side effects:
  2442.  *    None.
  2443.  *
  2444.  *----------------------------------------------------------------------
  2445.  */
  2446.  
  2447. Tk_Window
  2448. Tk_CoordsToWindow(rootX, rootY, tkwin)
  2449.     int rootX, rootY;        /* Coordinates of point in root window.  If
  2450.                  * a virtual-root window manager is in use,
  2451.                  * these coordinates refer to the virtual
  2452.                  * root, not the real root. */
  2453.     Tk_Window tkwin;        /* Token for any window in application;
  2454.                  * used to identify the display. */
  2455. {
  2456.     POINT pos;
  2457.     HWND hwnd;
  2458.     TkWinDrawable *twdPtr;
  2459.     TkWindow *winPtr;
  2460.  
  2461.     pos.x = rootX;
  2462.     pos.y = rootY;
  2463.     hwnd = WindowFromPoint(pos);
  2464.  
  2465.     twdPtr = TkWinGetDrawableFromHandle(hwnd);
  2466.     if (twdPtr && (twdPtr->type == TWD_WINDOW)) {
  2467.     winPtr = TkWinGetWinPtr(twdPtr);
  2468.     if (winPtr->mainPtr == ((TkWindow *) tkwin)->mainPtr) {
  2469.         return (Tk_Window) winPtr;
  2470.     }
  2471.     }
  2472.     return NULL;
  2473. }
  2474.  
  2475. /*
  2476.  *----------------------------------------------------------------------
  2477.  *
  2478.  * Tk_GetVRootGeometry --
  2479.  *
  2480.  *    This procedure returns information about the virtual root
  2481.  *    window corresponding to a particular Tk window.
  2482.  *
  2483.  * Results:
  2484.  *    The values at xPtr, yPtr, widthPtr, and heightPtr are set
  2485.  *    with the offset and dimensions of the root window corresponding
  2486.  *    to tkwin.  If tkwin is being managed by a virtual root window
  2487.  *    manager these values correspond to the virtual root window being
  2488.  *    used for tkwin;  otherwise the offsets will be 0 and the
  2489.  *    dimensions will be those of the screen.
  2490.  *
  2491.  * Side effects:
  2492.  *    Vroot window information is refreshed if it is out of date.
  2493.  *
  2494.  *----------------------------------------------------------------------
  2495.  */
  2496.  
  2497. void
  2498. Tk_GetVRootGeometry(tkwin, xPtr, yPtr, widthPtr, heightPtr)
  2499.     Tk_Window tkwin;        /* Window whose virtual root is to be
  2500.                  * queried. */
  2501.     int *xPtr, *yPtr;        /* Store x and y offsets of virtual root
  2502.                  * here. */
  2503.     int *widthPtr, *heightPtr;    /* Store dimensions of virtual root here. */
  2504. {
  2505.     TkWindow *winPtr = (TkWindow *) tkwin;
  2506.  
  2507.     *xPtr = 0;
  2508.     *yPtr = 0;
  2509.     *widthPtr = DisplayWidth(winPtr->display, winPtr->screenNum);
  2510.     *heightPtr = DisplayHeight(winPtr->display, winPtr->screenNum);
  2511. }
  2512.  
  2513. /*
  2514.  *----------------------------------------------------------------------
  2515.  *
  2516.  * Tk_MoveToplevelWindow --
  2517.  *
  2518.  *    This procedure is called instead of Tk_MoveWindow to adjust
  2519.  *    the x-y location of a top-level window.  It delays the actual
  2520.  *    move to a later time and keeps window-manager information
  2521.  *    up-to-date with the move
  2522.  *
  2523.  * Results:
  2524.  *    None.
  2525.  *
  2526.  * Side effects:
  2527.  *    The window is eventually moved so that its upper-left corner
  2528.  *    (actually, the upper-left corner of the window's decorative
  2529.  *    frame, if there is one) is at (x,y).
  2530.  *
  2531.  *----------------------------------------------------------------------
  2532.  */
  2533.  
  2534. void
  2535. Tk_MoveToplevelWindow(tkwin, x, y)
  2536.     Tk_Window tkwin;        /* Window to move. */
  2537.     int x, y;            /* New location for window (within
  2538.                  * parent). */
  2539. {
  2540.     TkWindow *winPtr = (TkWindow *) tkwin;
  2541.     register WmInfo *wmPtr = winPtr->wmInfoPtr;
  2542.  
  2543.     if (!(winPtr->flags & TK_TOP_LEVEL)) {
  2544.     panic("Tk_MoveToplevelWindow called with non-toplevel window");
  2545.     }
  2546.     wmPtr->x = x;
  2547.     wmPtr->y = y;
  2548.     wmPtr->flags |= WM_MOVE_PENDING;
  2549.     wmPtr->flags &= ~(WM_NEGATIVE_X|WM_NEGATIVE_Y);
  2550.     if ((wmPtr->sizeHintsFlags & (USPosition|PPosition)) == 0) {
  2551.     wmPtr->sizeHintsFlags |= USPosition;
  2552.     }
  2553.  
  2554.     /*
  2555.      * If the window has already been mapped, must bring its geometry
  2556.      * up-to-date immediately, otherwise an event might arrive from the
  2557.      * server that would overwrite wmPtr->x and wmPtr->y and lose the
  2558.      * new position.
  2559.      */
  2560.  
  2561.     if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  2562.     if (wmPtr->flags & WM_UPDATE_PENDING) {
  2563.         Tcl_CancelIdleCall(UpdateGeometryInfo, (ClientData) winPtr);
  2564.     }
  2565.     UpdateGeometryInfo((ClientData) winPtr);
  2566.     }
  2567. }
  2568.  
  2569. /*
  2570.  *----------------------------------------------------------------------
  2571.  *
  2572.  * TkWmProtocolEventProc --
  2573.  *
  2574.  *    This procedure is called by the Tk_HandleEvent whenever a
  2575.  *    ClientMessage event arrives whose type is "WM_PROTOCOLS".
  2576.  *    This procedure handles the message from the window manager
  2577.  *    in an appropriate fashion.
  2578.  *
  2579.  * Results:
  2580.  *    None.
  2581.  *
  2582.  * Side effects:
  2583.  *    Depends on what sort of handler, if any, was set up for the
  2584.  *    protocol.
  2585.  *
  2586.  *----------------------------------------------------------------------
  2587.  */
  2588.  
  2589. void
  2590. TkWmProtocolEventProc(winPtr, eventPtr)
  2591.     TkWindow *winPtr;        /* Window to which the event was sent. */
  2592.     XEvent *eventPtr;        /* X event. */
  2593. {
  2594.     WmInfo *wmPtr;
  2595.     register ProtocolHandler *protPtr;
  2596.     Atom protocol;
  2597.     int result;
  2598.     Tcl_Interp *interp;
  2599.  
  2600.     wmPtr = winPtr->wmInfoPtr;
  2601.     if (wmPtr == NULL) {
  2602.     return;
  2603.     }
  2604.     protocol = (Atom) eventPtr->xclient.data.l[0];
  2605.     for (protPtr = wmPtr->protPtr; protPtr != NULL;
  2606.         protPtr = protPtr->nextPtr) {
  2607.     if (protocol == protPtr->protocol) {
  2608.         Tcl_Preserve((ClientData) protPtr);
  2609.             interp = protPtr->interp;
  2610.             Tcl_Preserve((ClientData) interp);
  2611.         result = Tcl_GlobalEval(interp, protPtr->command);
  2612.         if (result != TCL_OK) {
  2613.         Tcl_AddErrorInfo(interp, "\n    (command for \"");
  2614.         Tcl_AddErrorInfo(interp,
  2615.             Tk_GetAtomName((Tk_Window) winPtr, protocol));
  2616.         Tcl_AddErrorInfo(interp, "\" window manager protocol)");
  2617.         Tcl_BackgroundError(interp);
  2618.         }
  2619.             Tcl_Release((ClientData) interp);
  2620.         Tcl_Release((ClientData) protPtr);
  2621.         return;
  2622.     }
  2623.     }
  2624.  
  2625.     /*
  2626.      * No handler was present for this protocol.  If this is a
  2627.      * WM_DELETE_WINDOW message then just destroy the window.
  2628.      */
  2629.  
  2630.     if (protocol == Tk_InternAtom((Tk_Window) winPtr, "WM_DELETE_WINDOW")) {
  2631.     Tk_DestroyWindow((Tk_Window) winPtr);
  2632.     }
  2633. }
  2634.  
  2635. /*
  2636.  *----------------------------------------------------------------------
  2637.  *
  2638.  * TkWmRestackToplevel --
  2639.  *
  2640.  *    This procedure restacks a top-level window.
  2641.  *
  2642.  * Results:
  2643.  *    None.
  2644.  *
  2645.  * Side effects:
  2646.  *    WinPtr gets restacked  as specified by aboveBelow and otherPtr.
  2647.  *    This procedure doesn't return until the restack has taken
  2648.  *    effect and the ConfigureNotify event for it has been received.
  2649.  *
  2650.  *----------------------------------------------------------------------
  2651.  */
  2652.  
  2653. void
  2654. TkWmRestackToplevel(winPtr, aboveBelow, otherPtr)
  2655.     TkWindow *winPtr;        /* Window to restack. */
  2656.     int aboveBelow;        /* Gives relative position for restacking;
  2657.                  * must be Above or Below. */
  2658.     TkWindow *otherPtr;        /* Window relative to which to restack;
  2659.                  * if NULL, then winPtr gets restacked
  2660.                  * above or below *all* siblings. */
  2661. {
  2662.     XWindowChanges changes;
  2663.     unsigned int mask;
  2664.     Window window;
  2665.  
  2666.     changes.stack_mode = aboveBelow;
  2667.     mask = CWStackMode;
  2668.     if (winPtr->window == None) {
  2669.     Tk_MakeWindowExist((Tk_Window) winPtr);
  2670.     }
  2671.     if (winPtr->wmInfoPtr->flags & WM_NEVER_MAPPED) {
  2672.     /*
  2673.      * Can't set stacking order properly until the window is on the
  2674.      * screen (mapping it may give it a reparent window), so make sure
  2675.      * it's on the screen.
  2676.      */
  2677.  
  2678.     TkWmMapWindow(winPtr);
  2679.     }
  2680.     window = (winPtr->wmInfoPtr->reparent != None)
  2681.         ? winPtr->wmInfoPtr->reparent : winPtr->window;
  2682.     if (otherPtr != NULL) {
  2683.     if (otherPtr->window == None) {
  2684.         Tk_MakeWindowExist((Tk_Window) otherPtr);
  2685.     }
  2686.     if (otherPtr->wmInfoPtr->flags & WM_NEVER_MAPPED) {
  2687.         TkWmMapWindow(otherPtr);
  2688.     }
  2689.     changes.sibling = (otherPtr->wmInfoPtr->reparent != None)
  2690.         ? otherPtr->wmInfoPtr->reparent : otherPtr->window;
  2691.     mask = CWStackMode|CWSibling;
  2692.     }
  2693.  
  2694.     /*
  2695.      * Reconfigure the window.  Since this is Windows, the
  2696.      * reconfiguration will happen immediately.
  2697.      */
  2698.  
  2699.     XConfigureWindow(winPtr->display, window, mask, &changes);
  2700. }
  2701.  
  2702. /*
  2703.  *----------------------------------------------------------------------
  2704.  *
  2705.  * TkWmAddToColormapWindows --
  2706.  *
  2707.  *    This procedure is called to add a given window to the
  2708.  *    WM_COLORMAP_WINDOWS property for its top-level, if it
  2709.  *    isn't already there.  It is invoked by the Tk code that
  2710.  *    creates a new colormap, in order to make sure that colormap
  2711.  *    information is propagated to the window manager by default.
  2712.  *
  2713.  * Results:
  2714.  *    None.
  2715.  *
  2716.  * Side effects:
  2717.  *    WinPtr's window gets added to the WM_COLORMAP_WINDOWS
  2718.  *    property of its nearest top-level ancestor, unless the
  2719.  *    colormaps have been set explicitly with the
  2720.  *    "wm colormapwindows" command.
  2721.  *
  2722.  *----------------------------------------------------------------------
  2723.  */
  2724.  
  2725. void
  2726. TkWmAddToColormapWindows(winPtr)
  2727.     TkWindow *winPtr;        /* Window with a non-default colormap.
  2728.                  * Should not be a top-level window. */
  2729. {
  2730.     TkWindow *topPtr;
  2731.     TkWindow **oldPtr, **newPtr;
  2732.     int count, i;
  2733.  
  2734.     if (winPtr->window == None) {
  2735.     return;
  2736.     }
  2737.  
  2738.     for (topPtr = winPtr->parentPtr; ; topPtr = topPtr->parentPtr) {
  2739.     if (topPtr == NULL) {
  2740.         /*
  2741.          * Window is being deleted.  Skip the whole operation.
  2742.          */
  2743.  
  2744.         return;
  2745.     }
  2746.     if (topPtr->flags & TK_TOP_LEVEL) {
  2747.         break;
  2748.     }
  2749.     }
  2750.     if (topPtr->wmInfoPtr->flags & WM_COLORMAPS_EXPLICIT) {
  2751.     return;
  2752.     }
  2753.  
  2754.     /*
  2755.      * Make sure that the window isn't already in the list.
  2756.      */
  2757.  
  2758.     count = topPtr->wmInfoPtr->cmapCount;
  2759.     oldPtr = topPtr->wmInfoPtr->cmapList;
  2760.  
  2761.     for (i = 0; i < count; i++) {
  2762.     if (oldPtr[i] == winPtr) {
  2763.         return;
  2764.     }
  2765.     }
  2766.  
  2767.     /*
  2768.      * Make a new bigger array and use it to reset the property.
  2769.      * Automatically add the toplevel itself as the last element
  2770.      * of the list.
  2771.      */
  2772.  
  2773.     newPtr = (TkWindow **) ckalloc((unsigned) ((count+2)*sizeof(TkWindow*)));
  2774.     if (count > 0) {
  2775.     memcpy(newPtr, oldPtr, count * sizeof(TkWindow*));
  2776.     }
  2777.     newPtr[count] = winPtr;
  2778.     newPtr[count+1] = topPtr;
  2779.     if (oldPtr != NULL) {
  2780.     ckfree((char *) oldPtr);
  2781.     }
  2782.  
  2783.     topPtr->wmInfoPtr->cmapList = newPtr;
  2784.     topPtr->wmInfoPtr->cmapCount = count+2;
  2785.  
  2786.     /*
  2787.      * Now we need to force the updated colormaps to be installed.
  2788.      */
  2789.  
  2790.     if (topPtr->wmInfoPtr == foregroundWmPtr) {
  2791.     TkWinWmInstallColormaps(TkWinGetHWND(topPtr->wmInfoPtr->reparent),
  2792.         WM_QUERYNEWPALETTE, 1);
  2793.     } else {
  2794.     TkWinWmInstallColormaps(TkWinGetHWND(topPtr->wmInfoPtr->reparent),
  2795.         WM_PALETTECHANGED, 0);
  2796.     }
  2797. }
  2798.  
  2799. /*
  2800.  *----------------------------------------------------------------------
  2801.  *
  2802.  * TkWmRemoveFromColormapWindows --
  2803.  *
  2804.  *    This procedure is called to remove a given window from the
  2805.  *    WM_COLORMAP_WINDOWS property for its top-level.  It is invoked
  2806.  *    when windows are deleted.
  2807.  *
  2808.  * Results:
  2809.  *    None.
  2810.  *
  2811.  * Side effects:
  2812.  *    WinPtr's window gets removed from the WM_COLORMAP_WINDOWS
  2813.  *    property of its nearest top-level ancestor, unless the
  2814.  *    top-level itself is being deleted too.
  2815.  *
  2816.  *----------------------------------------------------------------------
  2817.  */
  2818.  
  2819. void
  2820. TkWmRemoveFromColormapWindows(winPtr)
  2821.     TkWindow *winPtr;        /* Window that may be present in
  2822.                  * WM_COLORMAP_WINDOWS property for its
  2823.                  * top-level.  Should not be a top-level
  2824.                  * window. */
  2825. {
  2826.     TkWindow *topPtr;
  2827.     TkWindow **oldPtr;
  2828.     int count, i, j;
  2829.  
  2830.     for (topPtr = winPtr->parentPtr; ; topPtr = topPtr->parentPtr) {
  2831.     if (topPtr == NULL) {
  2832.         /*
  2833.          * Ancestors have been deleted, so skip the whole operation.
  2834.          * Seems like this can't ever happen?
  2835.          */
  2836.  
  2837.         return;
  2838.     }
  2839.     if (topPtr->flags & TK_TOP_LEVEL) {
  2840.         break;
  2841.     }
  2842.     }
  2843.     if (topPtr->flags & TK_ALREADY_DEAD) {
  2844.     /*
  2845.      * Top-level is being deleted, so there's no need to cleanup
  2846.      * the WM_COLORMAP_WINDOWS property.
  2847.      */
  2848.  
  2849.     return;
  2850.     }
  2851.  
  2852.     /*
  2853.      * Find the window and slide the following ones down to cover
  2854.      * it up.
  2855.      */
  2856.  
  2857.     count = topPtr->wmInfoPtr->cmapCount;
  2858.     oldPtr = topPtr->wmInfoPtr->cmapList;
  2859.     for (i = 0; i < count; i++) {
  2860.     if (oldPtr[i] == winPtr) {
  2861.         for (j = i ; j < count-1; j++) {
  2862.         oldPtr[j] = oldPtr[j+1];
  2863.         }
  2864.         topPtr->wmInfoPtr->cmapCount = count-1;
  2865.         break;
  2866.     }
  2867.     }
  2868. }
  2869.  
  2870. /*
  2871.  *----------------------------------------------------------------------
  2872.  *
  2873.  * TkWinWmConfigure --
  2874.  *
  2875.  *    Generate a ConfigureNotify event based on the current position
  2876.  *    information.  This procedure is called by TkWinTopLevelProc.
  2877.  *
  2878.  * Results:
  2879.  *    None.
  2880.  *
  2881.  * Side effects:
  2882.  *    Queues a new event.
  2883.  *
  2884.  *----------------------------------------------------------------------
  2885.  */
  2886.  
  2887. void
  2888. TkWinWmConfigure(winPtr, pos)
  2889.     TkWindow *winPtr;
  2890.     WINDOWPOS *pos;
  2891. {
  2892.     XEvent event;
  2893.     WmInfo *wmPtr;
  2894.     int width, height;
  2895.  
  2896.     if (winPtr == NULL) {
  2897.     return;
  2898.     }
  2899.  
  2900.     wmPtr = winPtr->wmInfoPtr;
  2901.  
  2902.     /*
  2903.      * If the window was just iconified, then we don't need to update
  2904.      * the geometry, just iconify the window.
  2905.      */
  2906.  
  2907.     if (IsIconic(TkWinGetHWND(wmPtr->reparent))) {
  2908.     if (wmPtr->hints.initial_state == NormalState) {
  2909.         IconifyWindow(winPtr);
  2910.     }
  2911.     return;
  2912.     } else if (wmPtr->hints.initial_state == IconicState) {
  2913.     DeiconifyWindow(winPtr);
  2914.     }
  2915.  
  2916.     width = pos->cx - wmPtr->borderWidth;
  2917.     height = pos->cy - wmPtr->borderHeight;
  2918.  
  2919.     /* 
  2920.      * Update size information from the event.  There are a couple of
  2921.      * tricky points here:
  2922.      *
  2923.      * 1. If the user changed the size externally then set wmPtr->width
  2924.      *    and wmPtr->height just as if a "wm geometry" command had been
  2925.      *    invoked with the same information.
  2926.      * 2. However, if the size is changing in response to a request
  2927.      *    coming from us (WM_SYNC_PENDING is set), then don't set wmPtr->width
  2928.      *    or wmPtr->height (otherwise the window will stop tracking geometry
  2929.      *    manager requests).
  2930.      */
  2931.  
  2932.     if (!(wmPtr->flags & WM_SYNC_PENDING)) {
  2933.     if ((width != winPtr->changes.width)
  2934.         || (height != winPtr->changes.height)) {
  2935.         if ((wmPtr->width == -1) && (width == winPtr->reqWidth)) {
  2936.         /*
  2937.          * Don't set external width, since the user didn't change it
  2938.          * from what the widgets asked for.
  2939.          */
  2940.         } else {
  2941.         if (wmPtr->gridWin != NULL) {
  2942.             wmPtr->width = wmPtr->reqGridWidth
  2943.             + (width - winPtr->reqWidth)/wmPtr->widthInc;
  2944.             if (wmPtr->width < 0) {
  2945.             wmPtr->width = 0;
  2946.             }
  2947.         } else {
  2948.             wmPtr->width = width;
  2949.         }
  2950.         }
  2951.         if ((wmPtr->height == -1) && (height == winPtr->reqHeight)) {
  2952.         /*
  2953.          * Don't set external height, since the user didn't change it
  2954.          * from what the widgets asked for.
  2955.          */
  2956.         } else {
  2957.         if (wmPtr->gridWin != NULL) {
  2958.             wmPtr->height = wmPtr->reqGridHeight
  2959.             + (height - winPtr->reqHeight)/wmPtr->heightInc;
  2960.             if (wmPtr->height < 0) {
  2961.             wmPtr->height = 0;
  2962.             }
  2963.         } else {
  2964.             wmPtr->height = height;
  2965.         }
  2966.         }
  2967.         wmPtr->configWidth = width;
  2968.         wmPtr->configHeight = height;
  2969.     }
  2970.     wmPtr->x = pos->x;
  2971.     wmPtr->y = pos->y;
  2972.     wmPtr->flags &= ~(WM_NEGATIVE_X | WM_NEGATIVE_Y);
  2973.     }
  2974.  
  2975.     /*
  2976.      * Update the shape of the contained window.
  2977.      */
  2978.  
  2979.     winPtr->changes.x = pos->x;
  2980.     winPtr->changes.y = pos->y;
  2981.     winPtr->changes.width = width;
  2982.     winPtr->changes.height = height;
  2983.     MoveWindow(TkWinGetHWND(winPtr->window), 0, 0, width, height, TRUE);
  2984.     
  2985.     /*
  2986.      * Generate a ConfigureNotify event.
  2987.      */
  2988.  
  2989.     event.type = ConfigureNotify;
  2990.     event.xconfigure.serial = winPtr->display->request;
  2991.     event.xconfigure.send_event = False;
  2992.     event.xconfigure.display = winPtr->display;
  2993.     event.xconfigure.event = winPtr->window;
  2994.     event.xconfigure.window = winPtr->window;
  2995.     event.xconfigure.border_width = winPtr->changes.border_width;
  2996.     event.xconfigure.override_redirect = winPtr->atts.override_redirect;
  2997.     event.xconfigure.x = pos->x;
  2998.     event.xconfigure.y = pos->y;
  2999.     event.xconfigure.width = width;
  3000.     event.xconfigure.height = height;
  3001.     if (winPtr->changes.stack_mode == Above) {
  3002.     event.xconfigure.above = winPtr->changes.sibling;
  3003.     } else {
  3004.     event.xconfigure.above = None;
  3005.     }
  3006.     Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
  3007. }
  3008.  
  3009. /*
  3010.  *----------------------------------------------------------------------
  3011.  *
  3012.  * IconifyWindow --
  3013.  *
  3014.  *    Put a toplevel window into the iconified state.
  3015.  *
  3016.  * Results:
  3017.  *    None.
  3018.  *
  3019.  * Side effects:
  3020.  *    Iconifies the window, possibly mapping it for the first time.
  3021.  *
  3022.  *----------------------------------------------------------------------
  3023.  */
  3024.  
  3025. void
  3026. IconifyWindow(winPtr)
  3027.     TkWindow *winPtr;        /* Window to be iconified. */
  3028. {
  3029.     WmInfo *wmPtr = winPtr->wmInfoPtr;
  3030.     wmPtr->hints.initial_state = IconicState;
  3031.     if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  3032.     if (wmPtr->withdrawn) {
  3033.         Tk_MapWindow((Tk_Window) winPtr);
  3034.         wmPtr->withdrawn = 0;
  3035.     } else {
  3036.         wmPtr->flags |= WM_SYNC_PENDING;
  3037.         CloseWindow(TkWinGetHWND(wmPtr->reparent));
  3038.         wmPtr->flags &= ~WM_SYNC_PENDING;
  3039.         XUnmapWindow(winPtr->display, winPtr->window);
  3040.     }
  3041.     }
  3042. }
  3043.  
  3044. /*
  3045.  *----------------------------------------------------------------------
  3046.  *
  3047.  * DeiconifyWindow --
  3048.  *
  3049.  *    Put a toplevel window into the deiconified state.
  3050.  *
  3051.  * Results:
  3052.  *    None.
  3053.  *
  3054.  * Side effects:
  3055.  *    Deiconifies the window, possibly mapping it for the first time.
  3056.  *
  3057.  *----------------------------------------------------------------------
  3058.  */
  3059.  
  3060. void
  3061. DeiconifyWindow(winPtr)
  3062.     TkWindow *winPtr;        /* Window to be deiconified. */
  3063. {
  3064.     WmInfo *wmPtr = winPtr->wmInfoPtr;
  3065.     wmPtr->hints.initial_state = NormalState;
  3066.     wmPtr->withdrawn = 0;
  3067.     if (!(wmPtr->flags & WM_NEVER_MAPPED)) {
  3068.     Tk_MapWindow((Tk_Window) winPtr);
  3069.     }
  3070. }
  3071.  
  3072. /*
  3073.  *----------------------------------------------------------------------
  3074.  *
  3075.  * TkWinWmInstallColormaps --
  3076.  *
  3077.  *    Installs the colormaps associated with the toplevel which is
  3078.  *    currently active.
  3079.  *
  3080.  * Results:
  3081.  *    None.
  3082.  *
  3083.  * Side effects:
  3084.  *    May change the system palette and generate damage.
  3085.  *
  3086.  *----------------------------------------------------------------------
  3087.  */
  3088.  
  3089. int
  3090. TkWinWmInstallColormaps(hwnd, message, isForemost)
  3091.     HWND hwnd;            /* Toplevel wrapper window whose colormaps
  3092.                  * should be installed. */
  3093.     int message;        /* Either WM_PALETTECHANGED or
  3094.                  * WM_QUERYNEWPALETTE */
  3095.     int isForemost;        /* 1 if window is foremost, else 0 */
  3096. {
  3097.     int i;
  3098.     HDC dc;
  3099.     HPALETTE oldPalette;
  3100.     TkWinDrawable *twdPtr =
  3101.     (TkWinDrawable *) GetWindowLong(hwnd, GWL_USERDATA);
  3102.     TkWindow *winPtr = TkWinGetWinPtr(twdPtr);
  3103.     WmInfo *wmPtr;
  3104.         
  3105.     if (winPtr == NULL) {
  3106.     return 0;
  3107.     }
  3108.  
  3109.     wmPtr = winPtr->wmInfoPtr;
  3110.  
  3111.     if (message == WM_QUERYNEWPALETTE) {
  3112.     /*
  3113.      * Case 1: This window is about to become the foreground window, so we
  3114.      * need to install the primary palette. If the system palette was
  3115.      * updated, then Windows will generate a WM_PALETTECHANGED message.
  3116.      * Otherwise, we have to synthesize one in order to ensure that the
  3117.      * secondary palettes are installed properly.
  3118.      */
  3119.  
  3120.     foregroundWmPtr = wmPtr;
  3121.  
  3122.     if (wmPtr->cmapCount > 0) {
  3123.         winPtr = wmPtr->cmapList[0];
  3124.     }
  3125.  
  3126.     systemPalette = TkWinGetPalette(winPtr->atts.colormap);
  3127.     dc = GetDC(hwnd);
  3128.     oldPalette = SelectPalette(dc, systemPalette, FALSE);
  3129.     if (RealizePalette(dc)) {
  3130.         RefreshColormap(winPtr->atts.colormap);
  3131.     } else if (wmPtr->cmapCount > 1) {
  3132.         SelectPalette(dc, oldPalette, TRUE);
  3133.         RealizePalette(dc);
  3134.         ReleaseDC(hwnd, dc);
  3135.         SendMessage(hwnd, WM_PALETTECHANGED, (WPARAM)hwnd,
  3136.             (LPARAM)NULL);
  3137.         return TRUE;
  3138.     }
  3139.  
  3140.     } else {
  3141.     /*
  3142.      * Window is being notified of a change in the system palette.
  3143.      * If this window is the foreground window, then we should only
  3144.      * install the secondary palettes, since the primary was installed
  3145.      * in response to the WM_QUERYPALETTE message.  Otherwise, install
  3146.      * all of the palettes.
  3147.      */
  3148.  
  3149.  
  3150.     if (!isForemost) {
  3151.         if (wmPtr->cmapCount > 0) {
  3152.         winPtr = wmPtr->cmapList[0];
  3153.         }
  3154.         i = 1;
  3155.     } else {
  3156.         if (wmPtr->cmapCount <= 1) {
  3157.         return TRUE;
  3158.         }
  3159.         winPtr = wmPtr->cmapList[1];
  3160.         i = 2;
  3161.     }
  3162.     dc = GetDC(hwnd);
  3163.     oldPalette = SelectPalette(dc,
  3164.         TkWinGetPalette(winPtr->atts.colormap), TRUE);
  3165.     if (RealizePalette(dc)) {
  3166.         RefreshColormap(winPtr->atts.colormap);
  3167.     }
  3168.     for (; i < wmPtr->cmapCount; i++) {
  3169.         winPtr = wmPtr->cmapList[i];
  3170.         SelectPalette(dc, TkWinGetPalette(winPtr->atts.colormap), TRUE);
  3171.         if (RealizePalette(dc)) {
  3172.         RefreshColormap(winPtr->atts.colormap);
  3173.         }
  3174.     }
  3175.     }
  3176.  
  3177.     SelectPalette(dc, oldPalette, TRUE);
  3178.     RealizePalette(dc);
  3179.     ReleaseDC(hwnd, dc);
  3180.     return TRUE;
  3181. }
  3182.  
  3183. /*
  3184.  *----------------------------------------------------------------------
  3185.  *
  3186.  * RefreshColormap --
  3187.  *
  3188.  *    This function is called to force all of the windows that use
  3189.  *    a given colormap to redraw themselves.  The quickest way to
  3190.  *    do this is to iterate over the toplevels, looking in the
  3191.  *    cmapList for matches.  This will quickly eliminate subtrees
  3192.  *    that don't use a given colormap.
  3193.  *
  3194.  * Results:
  3195.  *    None.
  3196.  *
  3197.  * Side effects:
  3198.  *    Causes damage events to be generated.
  3199.  *
  3200.  *----------------------------------------------------------------------
  3201.  */
  3202.  
  3203. static void
  3204. RefreshColormap(colormap)
  3205.     Colormap colormap;
  3206. {
  3207.     WmInfo *wmPtr;
  3208.     int i;
  3209.  
  3210.     for (wmPtr = firstWmPtr; wmPtr != NULL; wmPtr = wmPtr->nextPtr) {
  3211.     if (wmPtr->cmapCount > 0) {
  3212.         for (i = 0; i < wmPtr->cmapCount; i++) {
  3213.         if ((wmPtr->cmapList[i]->atts.colormap == colormap)
  3214.             && Tk_IsMapped(wmPtr->cmapList[i])) {
  3215.             InvalidateSubTree(wmPtr->cmapList[i], colormap);
  3216.         }
  3217.         }
  3218.     } else if ((wmPtr->winPtr->atts.colormap == colormap)
  3219.         && Tk_IsMapped(wmPtr->winPtr)) {
  3220.         InvalidateSubTree(wmPtr->winPtr, colormap);
  3221.     }
  3222.     }
  3223. }
  3224.  
  3225. /*
  3226.  *----------------------------------------------------------------------
  3227.  *
  3228.  * InvalidateSubTree --
  3229.  *
  3230.  *    This function recursively generates damage for a window and
  3231.  *    all of its mapped children that belong to the same toplevel and
  3232.  *    are using the specified colormap.
  3233.  *
  3234.  * Results:
  3235.  *    None.
  3236.  *
  3237.  * Side effects:
  3238.  *    Generates damage for the specified subtree.
  3239.  *
  3240.  *----------------------------------------------------------------------
  3241.  */
  3242.  
  3243. static void
  3244. InvalidateSubTree(winPtr, colormap)
  3245.     TkWindow *winPtr;
  3246.     Colormap colormap;
  3247. {
  3248.     TkWindow *childPtr;
  3249.  
  3250.     /*
  3251.      * Generate damage for the current window if it is using the
  3252.      * specified colormap.
  3253.      */
  3254.  
  3255.     if (winPtr->atts.colormap == colormap) {
  3256.     InvalidateRect(TkWinGetHWND(winPtr->window), NULL, FALSE);
  3257.     }
  3258.  
  3259.     for (childPtr = winPtr->childList; childPtr != NULL;
  3260.         childPtr = childPtr->nextPtr) {
  3261.     /*
  3262.      * We can stop the descent when we hit an unmapped or
  3263.      * toplevel window.  
  3264.      */
  3265.  
  3266.     if (!Tk_IsTopLevel(childPtr) && Tk_IsMapped(childPtr)) {
  3267.         InvalidateSubTree(childPtr, colormap);
  3268.     }
  3269.     }
  3270. }
  3271.  
  3272. /*
  3273.  *----------------------------------------------------------------------
  3274.  *
  3275.  * TkWinGetSystemPalette --
  3276.  *
  3277.  *    Retrieves the currently installed foreground palette.
  3278.  *
  3279.  * Results:
  3280.  *    Returns the global foreground palette, if there is one.
  3281.  *    Otherwise, returns NULL.
  3282.  *
  3283.  * Side effects:
  3284.  *    None.
  3285.  *
  3286.  *----------------------------------------------------------------------
  3287.  */
  3288.  
  3289. HPALETTE
  3290. TkWinGetSystemPalette()
  3291. {
  3292.     return systemPalette;
  3293. }
  3294.  
  3295. /*
  3296.  *----------------------------------------------------------------------
  3297.  *
  3298.  * GetMinSize --
  3299.  *
  3300.  *    This procedure computes the current minWidth and minHeight
  3301.  *    values for a window, taking into account the possibility
  3302.  *    that they may be defaulted.
  3303.  *
  3304.  * Results:
  3305.  *    The values at *minWidthPtr and *minHeightPtr are filled
  3306.  *    in with the minimum allowable dimensions of wmPtr's window,
  3307.  *    in grid units.  If the requested minimum is smaller than the
  3308.  *    system required minimum, then this procedure computes the
  3309.  *    smallest size that will satisfy both the system and the
  3310.  *    grid constraints.
  3311.  *
  3312.  * Side effects:
  3313.  *    None.
  3314.  *
  3315.  *----------------------------------------------------------------------
  3316.  */
  3317.  
  3318. static void
  3319. GetMinSize(wmPtr, minWidthPtr, minHeightPtr)
  3320.     WmInfo *wmPtr;        /* Window manager information for the
  3321.                  * window. */
  3322.     int *minWidthPtr;        /* Where to store the current minimum
  3323.                  * width of the window. */
  3324.     int *minHeightPtr;        /* Where to store the current minimum
  3325.                  * height of the window. */
  3326. {
  3327.     int tmp, base;
  3328.     TkWindow *winPtr = wmPtr->winPtr;
  3329.  
  3330.     /*
  3331.      * Compute the minimum width by taking the default client size
  3332.      * and rounding it up to the nearest grid unit.  Return the greater
  3333.      * of the default minimum and the specified minimum.
  3334.      */
  3335.  
  3336.     tmp = wmPtr->defMinWidth - wmPtr->borderWidth;
  3337.     if (tmp < 0) {
  3338.     tmp = 0;
  3339.     }
  3340.     if (wmPtr->gridWin != NULL) {
  3341.     base = winPtr->reqWidth - (wmPtr->reqGridWidth * wmPtr->widthInc);
  3342.     if (base < 0) {
  3343.         base = 0;
  3344.     }
  3345.     tmp = ((tmp - base) + wmPtr->widthInc - 1)/wmPtr->widthInc;
  3346.     }
  3347.     if (tmp < wmPtr->minWidth) {
  3348.     tmp = wmPtr->minWidth;
  3349.     }
  3350.     *minWidthPtr = tmp;
  3351.  
  3352.     /*
  3353.      * Compute the minimum height in a similar fashion.
  3354.      */
  3355.  
  3356.     tmp = wmPtr->defMinHeight - wmPtr->borderHeight;
  3357.     if (tmp < 0) {
  3358.     tmp = 0;
  3359.     }
  3360.     if (wmPtr->gridWin != NULL) {
  3361.     base = winPtr->reqHeight - (wmPtr->reqGridHeight * wmPtr->heightInc);
  3362.     if (base < 0) {
  3363.         base = 0;
  3364.     }
  3365.     tmp = ((tmp - base) + wmPtr->heightInc - 1)/wmPtr->heightInc;
  3366.     }
  3367.     if (tmp < wmPtr->minHeight) {
  3368.     tmp = wmPtr->minHeight;
  3369.     }
  3370.     *minHeightPtr = tmp;
  3371. }
  3372.  
  3373. /*
  3374.  *----------------------------------------------------------------------
  3375.  *
  3376.  * GetMaxSize --
  3377.  *
  3378.  *    This procedure computes the current maxWidth and maxHeight
  3379.  *    values for a window, taking into account the possibility
  3380.  *    that they may be defaulted.
  3381.  *
  3382.  * Results:
  3383.  *    The values at *maxWidthPtr and *maxHeightPtr are filled
  3384.  *    in with the maximum allowable dimensions of wmPtr's window,
  3385.  *    in grid units.  If no maximum has been specified for the
  3386.  *    window, then this procedure computes the largest sizes that
  3387.  *    will fit on the screen.
  3388.  *
  3389.  * Side effects:
  3390.  *    None.
  3391.  *
  3392.  *----------------------------------------------------------------------
  3393.  */
  3394.  
  3395. static void
  3396. GetMaxSize(wmPtr, maxWidthPtr, maxHeightPtr)
  3397.     WmInfo *wmPtr;        /* Window manager information for the
  3398.                  * window. */
  3399.     int *maxWidthPtr;        /* Where to store the current maximum
  3400.                  * width of the window. */
  3401.     int *maxHeightPtr;        /* Where to store the current maximum
  3402.                  * height of the window. */
  3403. {
  3404.     int tmp;
  3405.  
  3406.     if (wmPtr->maxWidth > 0) {
  3407.     *maxWidthPtr = wmPtr->maxWidth;
  3408.     } else {
  3409.     /*
  3410.      * Must compute a default width.  Fill up the display, leaving a
  3411.      * bit of extra space for the window manager's borders.
  3412.      */
  3413.  
  3414.     tmp = wmPtr->defMaxWidth - wmPtr->borderWidth;
  3415.     if (wmPtr->gridWin != NULL) {
  3416.         /*
  3417.          * Gridding is turned on;  convert from pixels to grid units.
  3418.          */
  3419.  
  3420.         tmp = wmPtr->reqGridWidth
  3421.             + (tmp - wmPtr->winPtr->reqWidth)/wmPtr->widthInc;
  3422.     }
  3423.     *maxWidthPtr = tmp;
  3424.     }
  3425.     if (wmPtr->maxHeight > 0) {
  3426.     *maxHeightPtr = wmPtr->maxHeight;
  3427.     } else {
  3428.     tmp = wmPtr->defMaxHeight - wmPtr->borderHeight;
  3429.     if (wmPtr->gridWin != NULL) {
  3430.         tmp = wmPtr->reqGridHeight
  3431.             + (tmp - wmPtr->winPtr->reqHeight)/wmPtr->heightInc;
  3432.     }
  3433.     *maxHeightPtr = tmp;
  3434.     }
  3435. }
  3436.